Pages

Thursday, September 8, 2016

while loop (java)

While loop

While loop is the same as for loop , the syntax are different and as well as it is necessary to use while loop in some places.While is the identity of this loop as for of the for loop.The syntax of this loop is given below



A variable for the while loop is initiallized before the while loop and it increments in the body .And the condition statement is in the rount braces

ie




 // variable initialization 
  int i = 0;
 
  /*while loop  name
   *  and
   *  condition in
   *  round braces
   */
 while(i<=20){
              
System.out.println("Assalam-o-Alaikum");
   
/* incrementation*/  
        i ++;
   }

     



The out put of the above programm is as follow



Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum
Assalam-o-Alaikum






If you will notice the above example . The variable has been initiallized before the while loop condition out of round braces So here the execution of while loop is as follow

1. Compiler 1st start with variable initialization. It takes a look at the initialization

2. And then compiler straight goes to the next part, which is the condition part in the above example ( i <= 20 ) is the condition part. In this part the compiler check, Wether is the initiallized value is coming true with this statement . In the above example if the initiallized value is less then 20 .So thats mean the statement the while condition (statement in the round braces) is true).

3. As it becomes true .Compiler goes to step three which is body, ( body is the part which is sorround with curly braces ie { } ). What ever in the body is, it executes .

4.In the body , In last there is another statement for compiler to be executed which is known as incrementation part in this part the variable changes to another value( it increments by the value given in the last of the while loop ). In the above example it increments by i++ thats mean it will add only one to the current variable which we initiallized in the start ( which was 0 ) , As it increments , So the value of this variable is now changed to 1.

5. Compiler goes back to the condition statement and check again for the condition , Is the statement true again , If it is true ,So it goes again to the body and executes body codes and icrements by 1 again .And this proccess is going on until the while condition becomes false as the variable value reach the maximum 21 , so while loop then refuse to executes body codes after 21 times.



Example 2:

Write down a program, In which print out even number upto 100.



  int i = 0;


  while(i<= 100){

  System.out.println(i);

     i = i + 2;
          

 }



Out Put :

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

No comments:

Post a Comment