Wednesday, 4 November 2020

The while Loop in JAVA

 

 

The while Loop  in JAVA 


A while loop is a control structure  that  allows us to repeat a task a certain number of times.


The syntax of a while  loop  is :-

while(boolean_expression)

{

       //Statements

  }


When executing ,  if the boolean _expression  result  is true, then  the action  inside the loop will be executed. This  will  continue as long  as the expression result is  true.Here  key  point  of  the while loop  is that the loop  might  not  ever run . When the expression  is  tested  and the result  is false, the loop body  will be skipped and the first statement  after the while  loop  will be executed.


EXAMPLE :-

 

  public class Test 

 {

     public static void main(String args[])

     {

         int x =10;

         while(x<15)

            {

                System .out.print("value of x :"+x);

                 x++;

                System.out.print("\n");

           

              }

        }

 }


OUTPUT :-

value of x :10

value of x: 11

value of x: 12

value of x: 13

value of x: 14


 


No comments:

Post a Comment

Student Marks Calculation app using java

      import javax.swing. *; import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; public class Student { private J...