Search Your Topic

config

Java While Loop

Written by
Rate this item
(0 votes)

The Java while loop is used to iterate a part of the program several times. If the number of iteration is not fixed, it is recommended to use while loop.

Syntax:

 
  1. while(condition){  
  2. //code to be executed  
  3. }  

flowchart of java while loop

Example:

 
  1. public class WhileExample {  
  2. public static void main(String[] args) {  
  3.     int i=1;  
  4.     while(i<=10){  
  5.         System.out.println(i);  
  6.     i++;  
  7.     }  
  8. }  
  9. }  

Output:

1
2
3
4
5
6
7
8
9
10


Java Infinitive While Loop

If you pass true in the while loop, it will be infinitive while loop.

Syntax:

 
  1. while(true){  
  2. //code to be executed  
  3. }  

Example:

 
  1. public class WhileExample2 {  
  2. public static void main(String[] args) {  
  3.     while(true){  
  4.         System.out.println("infinitive while loop");  
  5.     }  
  6. }  
  7. }  

Output:

infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

Now, you need to press ctrl+c to exit from the program

Read 375 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « Java For Loop Java do-while Loop »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.

Java Training

Facebook