While loop statements in Java

In previous article we have covered for loop here we will discuss about the other type of looping statement WHILE. WHILE statement is useful when you want to execute something till a particular condition is true.

Scenario of WHILE loop

Ask user to provide input and wait till user provide the input.

Syntax of WHILE loop


    while (expression) {
         //Do Something
     }

Here while statement will evaluate the expression and check if it is true or not. If true then execute the statement within loop. Once completed execution in loop again check the expression. Perform this activity unless value of expression is not TRUE.

Example Code WHILE loop

 


class JBT_WhileLoopExample {

	public static void main(String[] args) {

		System.out.println("While For Loop Example");

		boolean bool = true;

		/*
		 * Here while will check the expression. bool value is TRUE so statement
		 * within loop will get executed. Inside loop for first iteration, value of bool is set to
		 * FALSE. Hence in next iteration while loop will check if value of bool
		 * is true or false, now it is FALSE so there will not be next execution
		 * within loop.
		 */
		while (bool) {
			System.out.println("Expression value is TRUE");
			bool = false;
		}

		System.out.println("Expression value is FALSE NOW");
	}

}

Output of above code would be


While For Loop Example
Expression value is TRUE now
Expression value is now FALSE
By | 2013-03-17T14:46:36+00:00 March 17th, 2013|Learn By Example|7 Comments

About the Author:

7 Comments

  1. Amain December 13, 2013 at 6:07 pm - Reply

    Sir,
    I would like to confirm if body of the while loop gets executed if conditional test evaluates to true or false, because when i executed the code as given on this page ,the output was different than as given by you.Acc yo ur expalnation body of while loop gets executed if test is false ,rather it shud b true.Am i right?

    • Vivekanand Gautam December 15, 2013 at 1:31 pm - Reply

      Hi Amain,

      Example given in article is correct. As per your question block will get executed till condition is true. Moment it becomes false execution will come out of block.

      As flag was false in first time so it didn’t go inside block. I have changed the code block so that you can understand clearly.

      Please check it again.

      Thanks

  2. Amain December 15, 2013 at 10:36 am - Reply

    Sir,
    I suppose you have changed the code . Thank you for that. But I suppose that the explanation that you have given in the code in form of the comments also needs to be changed and sir I suppose the first line of the explanation is wrong if I am not wrong.

    /*
    * Here while will check the expression. value is “false” so statement
    * within loop will get executed.

    It was this statement I was talking about which seems wrong to me. As i have quoted “false” should be replaced with “true”.

    • Vivekanand Gautam December 15, 2013 at 1:32 pm - Reply

      Thanks for pointing this problem. Same has been updated. 🙂

  3. aditya vardhan May 3, 2015 at 1:21 pm - Reply

    Need correction:
    Error:Here while statement will evaluate the expression and check if it is true or not. If NOT then execute the statement within loop
    Correct:Here while statement will evaluate the expression and check if it is true or not. If TRUE then execute the statement within loop

    • V Gautam May 3, 2015 at 4:26 pm - Reply

      Article Updated.

  4. Anon November 26, 2016 at 2:05 am - Reply

    This site is stupid, basic algorithmic stuff like while loops is at the end, but advanced topics like objects are put in the beginning….. How does one expect people to learn from that.

Leave A Comment