DO WHILE loop

The do while condition executes the body at least once, it…. does this before the loop is first tested.

class jamesprogram {

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

Outputs:

0
1
2
3
4
5
6
7
8
9

However if we change the count initialization to 12

class jamesprogram {

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

The output just becomes

12

The println is executed once, then it’s tested, is count<12, it is not, so the while loop ends.

Leave a Reply