While Loop Java

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

while loop java

What is while loop in java programming??

While loop statement in java continually executes block of statements while a particular condition is true.

Syntax :

while (expression)
{
   statement 1;
   statement 2;
}

While statement in java evaluates expression which return a boolean value. If the expression evaluates to true, while statement executes the statements in the while block.

Also Read – Switch Statement Java

While statement continues to test the expression and executes while block until the expression evaluates to false.

Flow Diagram :

while loop java

Now I will show you one example,

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

Output :

10 
9
8
7
6
5
4
3
2


While loop array

Now we are going to iterate and display while loop array using an example,

public class WhileLoopArray 
{
   public static void main(String[] args) 
   {
      int arrNum[] = {3, 23, 41, 90};
      System.out.println("Java while loop");
      int a = 0;
      while(a < 4)
      {
         System.out.println(arrNum[a]);
         a++;
      }
   }
}

Output :

Java while loop
3
23
41
90


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 = 0;
      while(x < 10)
      {
         System.out.println(x);
      }
   }
}

In the above example I’m checking whether the value of ‘x’ is less than 10 or not. And right now I have initialized it by 0 so obviously it is less than 10.

Then it will be executed forever if I don’t change the value of ‘x’.  So above example can be an infinite loop. Infinite loops are also useful if you know how to use them.


Why would you use a while loop?

But right now we will see how to use condition more effectively.

Let’s see 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 then it goes to the condition again and 1 is less than 10 then it prints the value of ‘x’ once again and it prints 1.

Also Read – If Else Java

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.

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 you can do or execute your code like above.


conclusion

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