Thu. Mar 28th, 2024

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

Syntax:

While(Condition){
   Statements;
}

Program:

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

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

Rule2: Specifying the { } is optional. If we do not specify the { } then it will consider only one statement, and that one statement is mandatory. If we want to consider mutliple statements then specifying the { } is mandatory. within the { } we can specify any number of statements.

Note: A java program should not contain any unreachable statements i.e. every statement has to be executed at some point of time.

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 *