Google+

56. 'while' iteration statements







Iteration statement creates loops. A loop repeatedly executes the same set of instructions until a termination condition is met. Iteration statements are another type of 'Control Flow' statements.

 Program to demonstrate the 'while' loop                                                                                                                                

'while' loop executes the statements in its block repetitively as long as the condition expression is true.

class WhileLoop
{
   public static void main(String args[])
   {

      int i=1;

      while(i<6)    //This loop will be executed repeatedly until the value of i is less than 6
      {
         System.out.println("The value of i is "+i);
         i++;
      }

    }

 }

Output of this program:

The value of i is 1
The value of i is 2
The value of i is 3
The value of i is 4
The value of i is 5

'while' loop executes the statements inside its block repeatedly until the value of i is less than 6 in this example. So to start with 'while(i<6) i.e. while(1<6) which is true, hence it enters into the while block and prints "The value of i is 1" and then executes i++; i.e. it increments the value of i by 1. So the value of i now is 2. The loop then gets repeated with while(i<6) i.e. while(2<6) which is true -> hence prints "The value of i is 2", while(3<6) which is true -> hence prints "The value of i is 3", while(4<6) which is true -> hence prints "The value of i is 4", while(5<6) which is true -> hence prints "The value of i is 5" and while(6<6) which is false hence wont enter into the while block as the condition is false.

Program to demonstrate the infinite times executed while loop

'while' block will get executed infinite times if the condition expression is always true

class InfiniteWhileLoop
{
   public static void main(String args[])
   {

      int i=1;

      while(i<6)
      {
         System.out.println("The value of i is "+i);
      }

    }

 }

Output of this program:

"The value of i is 1" will be printed infinite times.

In this example, we've not incremented the value of i by 1 as we did in the earlier example. As a result the value of i is always 1. Since while(i<6) i.e. while(1<6) is always true, the statement "The value of i is 1" inside the while loop will be printed infinite times.

Program to demonstrate a never executed while loop

'while' block wont be executed at least single time when the condition expression is false.

class NeverExecutedWhileLoop
{
   public static void main(String args[])
   {

      int i=6;

      while(i<6)
      {
         System.out.println("The value of i is "+i);
      }

    }

 }

Output of this program:

[No output] - Since while(i<6) i.e. while(6<6) is false, the statements inside the while block wont be executed at least once.

Finding the mid value of 1 and 11 using the 'while' loop

class ExampleWhileLoop
{
   public static void main(String args[])
   {

      int i=1,j=11;

      while(++i<--j);   //There are no statements in the while loop, hence we have not used the flower braces.

      System.out.println("The mid value of 1 and 11 is "+i);

    }

 }

The output of this program:

The mid value of 1 and 11 is 6





Please comment below to feedback or ask questions.

How to use the do-while loop will be explained in the next post.




12 comments:

Unknown said...

Arun,

I have started learning JAVA required for Selenium Webdriver from your blog.
I'm a little confused as to where JAVA posts stop in the content. There are 257 chapters at present. Please let me know on which is the last chapter for JAVA and what are the remaining for.Also the Webdriver blogs.

Thanks
Soumya

Unknown said...

For below program, System.out.println("The mid value of 1 and 11 is "+i);
will be executed only when we get false for the while condition ?

when ever we reahced while (6<6) ; now the condition is false so the println statement was executed .

class ExampleWhileLoop
{
public static void main(String args[])
{

int i=1,j=11;

while(++i<--j); //There are no statements in the while loop, hence we have not used the flower braces.

System.out.println("The mid value of 1 and 11 is "+i);

}

}

Unknown said...

@Soumya Prabhakar: In Selenium 2 blogpost, in per-requisites for learning Selenium Webdriver Arun mentioned --- "Please go through all the 'Java for Selenium' posts (i.e. from Post#28 Java for Selenium to Post# 157 Using lastIndexOf( ) method with StringBuffer )".

Unknown said...

Hi Arun,

First of all I wondered the way of your explanation in such a practical manner.Beautiful blog for selenium beginners much appreciated work

Could you tell me the difference between while(); and while() What is the use of this semicolon after the while()?

'while' loop executes the statements in its block repetitively as long as the condition expression is true but in your mid value program will be executed only when we get false for the while condition ?

Ex:
Finding the mid value of 1 and 11 using the 'while' loop

With semicolon Output:

The mid value of 1 and 11 is i: 6
The mid value of 1 and 11 is j: 6

Without semicolon Output:

The mid value of 1 and 11 is i: 2
The mid value of 1 and 11 is i: 3
The mid value of 1 and 11 is i: 4
The mid value of 1 and 11 is i: 5
The mid value of 1 and 11 is j: 6

I hope this will be answer for PREETI ARUN question too.Actively looking for your reply

Soumya P said...

Hi Satish and Preeti,

As mentioned by Arun in the comment
//There are no statements in the while loop, hence we have not used the flower braces.

In this example there are no statements for the "While" loop.

That is the reason we had a semicolon at the end of While.

When the condition failed

System.out.println("The mid value of 1 and 11 is "+i);

got executed. Hope you understand.

Soumya

Arun Motoori said...

@Preeti and Satheesh -

In case of while(++i<--j);

As there are no statements in the while loop, the i gets incremented and j gets decremented until the while condition fails i.e. (6<6)

Hence at this time i holds 6 and j holds 6.

Now we are printing the value of i i.e. 6 which is nothing but the mid value of 1 and 11.

Rakesh said...

class Whilemid
{
public static void main(String args[])
{
int i=1,j=11;

while(++i<--j);

System.out.println("The mid value of 1 & 11 is "+i);
}
}

The output here is 7. i++ or ++i ---> The returning of the values matter here. Proper answer is 6 if we give ++i and --j.

Arun Motoori said...

@Rakesh - Yes, you are right. i++ is a post increment operator where as ++i is pre increment operators.

while(++i<--j); -> In this case i will be incremented and j will be decremented before comparing whether i less than j,

Where as using while(i++<--j); -> In this case i will be incremented after comparing with j i.e. after comparing whether i less than j. But j will be decremented prior to comparision.

Hence you are getting different outputs.

while(++i<--j); will give the correct output in our case.

Unknown said...

Hi Arun,
In your first example i have coded as below but eclipse dont give any results
while (i<6);
{
System.out.println("The value of i is " +i);
i++;
}

Arun Motoori said...

@Chandan Mohanty - Replace while(i<6); with while(i<6) in your program.

Anonymous said...

@arun- please explain the difference b/w while(i<6); & while(i<6).

Arun Motoori said...

while(i<6); without any body will result in infinite loop as there is no way that the value of i can be incremented.

while(i<6){ } having a body, can be used to write the program to increment the value of i inside its body, using which the loop ends when the value of i reaches 6.

Regards,
Arun