Tue. Dec 3rd, 2024

This loop can be used for executing the statements multiple times. A do-while loop has to be used when we do not know the exact number of iterations.

Syntax:

do{
   statements;
 }
while(condition);

Program 8:

class DoWhileDemo{
   public static void main(String args[]){
      int n=9,i=1;
      do{
      System.out.println(n+"*"+i+=+(n*i)));
      i++;
      } while (i<=10);
   }
}

Rule1: Specifying a condition to do-while loop is mandatory and it should be of boolean type.

Rule2: Specifying the { } is optional. If we do not specify the { } then we must specify exactly only one statement. If we want to consider multiple statements then specifying the { } is mandatory. Within the { } we can specify any number of statements.

Difference between while loop & do-while loop

  1. In a while loop the statements will execute after evaluating the condition, whether in a do-while loop the statements will exexute before evaluating the condition.
  2. In a while loop if the condition is false for the first time then the statement will not execute, whereas in a do-while loop if the first time then the statements will execute for one time.
  3. In a while loop the statements will execute for 0 or more times, whereas in a do-while loop the statements will execute for 1 or more times.

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *