While loop java

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn while loop java.

while loop java

While loop is nothing but which allows you to run/iterate a block of statements for a number of times, until expression is true.

Syntax :

while (expression)
{
   // code goes here
}

while loop java

public class WhileLoopDemo 
{
   public static void main(String args[])
   {
      int a = 1;
      while(a <= 10)
      {
         System.out.println(a);
         a++;
      }
   }
}

Output :

1

2

3

4

5

6

7

8

9

10


While loop java : infinite while loop

Now let’s see infinite while loop java with an example,

public class WhileLoopExample 
{
   public static void main(String[] args) 
   {
      int x = 10;
      while(x > 1)
      {
         System.out.println(x);
x++ } } }

In the above example x > 1 results in infinite loop.


Why use a while loop?

Let’s understand with an example,

public class WhileLoopExample 
{
   public static void main(String[] args) 
   {
      int x = 0;
      while(x < 10)
      {
         System.out.println(x);
         x++;
      }
   }
}

Output:

while loop java

As you can see the output the value of ‘x’ is increased by one. So then after running the program it did the same execution of code again and again unless test condition has met.

So what this code has done initially is; the value of ‘x’ was zero so it has printed the value of ‘x’ as 0 because 0 is less than 10.

Then it increased the value of ‘x’ by 1 and now the value of ‘x’ became 1. And it executes the same statement again and again and it increases the value of ‘x’ again and again until the value of ‘x’ becomes 10.

Also read – if else java

So it printed the value of ‘x’ until 9. But when the value of ‘x’ becomes 10 in the condition, 10 is not less than 10 then the condition is false and it will go out of the loop and program is terminated.

In the same way if you do something like below,

public class WhileLoopExample 
{
   public static void main(String[] args) 
   {
      int x = 0;
      while(x <= 10)
      {
         System.out.println(x);
         x++;
      }
   }
}

Output:

0
1
2
3
4
5
6
7
8
9
10

In the above example the condition “x” is less than or equal to 10. Then the above example will print the value of ‘x’ until 10.

And we have done using only a single line of code.

System.out.println(x);

We have printed ten or eleven values using a single line of code using loop. We are just incrementing the value of ‘x’ on the basis of

x++;

We are changing the value of ‘x’ and then we are checking these conditions

while(x <= 10)

This will work in opposite direction also. For example,

public class WhileLoopExample 
{
   public static void main(String[] args) 
   {
      int x = 10;
      while(x >= 1)
      {
         System.out.println(x);
         x--;
      }
   }
}

Here in the above condition when you change it to x >= 1 then the output will be,

Output:

10
9
8
7
6
5
4
3
2
1

It has printed the value from 10 to 1 in decreasing order. 


So, what basically this while loop is doing is it’s executing the code or these two lines

System.out.println(x);
x--;

again and again until and unless the condition is true. So if condition is true the statements will be executed.

As soon as the condition becomes false the while loop will break and it will go out of the loop. So this is the basic concept of while loop java.

I hope you have understood the basic concept. Do share this post if you like.

Related Posts