While Structure in Java

While it is one of the frequently used repetition structures in Java programming language. The process repeats as the boolean value returns.
 

example:

while(true){
  System.out.println("Forever again");
}

output:

Forever again
Forever again
Forever again
Forever again
Forever again
Forever again
Forever again
...

 

another example:

public class Main
{
	public static void main(String[] args) {
		int count =1;
        while(count<=5){
            System.out.println(count);
            count++;
        }
	}
}

output:

1
2
3
4
5

 

Another use of while repetition structure is do while structure. The difference of the do while structure from the while structure is that it has to enter into the loop at least once.

example:

public class Main
{
	public static void main(String[] args) {
		int count =1;
		do {
		    System.out.println(count);
		    count++;
        } while(count<=10);
	}
}

output:

1
2
3
4
5
6
7
8
9
10

 

 

Tagged In:

Software developer

26 Total Posts