There are 3 loops
a. while
b. do while
c. for
the difference between while and do while is that in while first the condition is checked and then the statements are executed but in do while at least once the statements are executed and then the condition is checked.
consider below example. i am printing 1 to 5 numbers using while loop
int i=1; while (i<=5) { System.out.println(i); i++; }
in first iteration i=1 which is less than 5 so the condition is true the control enters into the loop and executes the statements. it prints i value i.e. 1 and then increments i. and this loop continues till i=5.
now consider the same example with do while
int i=1; do { System.out.println(i); i++; }while (i<=5)
Here first once the 2 statements inside while loop will execute, then the condition will be checked whether the i is less than 5 or not and it will continue till i=5.
for loop syntax is
for(initialazation ; condition; increment or decrement)
lets see the same example using for loop
for (int i=0;i<5;i++) { System.out.println(i); }
the initialization and increment decrements in for loop are taken implicitly. while that of while and do while loop it is taken explicitly.
lets see one example .
find out even and odd numbers from 1 to 10
for (i=1;i<=10;i++) { if(i%2==0) System.out.println(i+" is even no."); else System.out.println(i+" is odd no."); }