- do-while loop:
This loop is similar to a while loop except for the fact that it is guaranteed to execute at least once
/* do {
//code
} while (condition); //Note this semicolon */
- while – checks the condition & executes the code
- do-while – executes the code & then checks the condition
Quick Quiz: Write a program to print first n natural numbers using a do-while loop.
package com.company;
public class cwh_22_ch4_do_while {
public static void main(String[] args) {
// int a = 0;
// while(a<5){
// System.out.println(a);
// a++;
// }
int b = 10;
do {
System.out.println(b);
b++;
}while(b<5);
int c = 1;
do{
System.out.println(c);
c++;
}while(c<=45);
}
}