Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
while loop in java - Testingpool

while loop in java

In java, while loop is used to executes same set of statements until condition becomes true.

Syntax of while loop:

while (condition(s)) {
// set of statements
}
Output: It is also called ‘entry controlled loop’ because conditions is checked at the entrance of the loop and it enters the loop only if condition is true.

Condition argument returns boolean value like true or false depending on the condition check. Once condition becomes false, control will jump to the next line of loop after exiting the loop.

public class WhileLoopEx {

	public static void main(String[] args) {
		
	       int counter = 1;
	        while (counter < 10) {  // print the number till 9
	            System.out.println("Number : " + counter);
	            counter++;
	        }
	}
}
Output:
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
Number : 6
Number : 7
Number : 8
Number : 9

If there is only one line statements, then curly braces are not required in the loop.

public class WhileLoopEx {

	public static void main(String[] args) {
		
	       int counter = 1;
	        while (counter < 10)   // no need of curly braces
	            System.out.println("Number : " + counter++);
	        
	}
}
Output:
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
Number : 6
Number : 7
Number : 8
Number : 9
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...

2 Responses

  1. August 9, 2015

    […] the previous post , we have seen while- loop. In this post, we will see Do..while […]

  2. August 10, 2015

    […] previous posts, we have seen while loop and Do ..while loop. In this post, we will see working of for […]