26 Jan 2017

What is while loop..??

As you know that , loop is the executing of set of statement in multiple time. The while loop is same as for loop but the syntax is little difference. The while loop is also called entry restricted loop.

As you know that , all the loop statements are under the control statements, so lets checkout the syntax of while loop:

initialization;

while (condition)
{
statements;
}

increment;

Lets take an example of while loop :

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a); // you can also use increment direct in print() method .
}
a++; // increment
}
}

Output :
1
2
3
4
5
6
7
8
9
10



Now the question arises why while loop called as entry restricted..??

Because the entry in loop is possible when the condition is true otherwise you cannot enter in the loop that is why while loop is also called entry restricted loop.

Lets take some other Examples:

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(a++); //increment passes in the println() method
}
}
}

Output is same as above appear.

class mobile
{public static void main(String ar[])
{int a=1;
   while(a<=10)
{
System.out.println(++a);
}
}
}

Output:
2
3
4
5
6
7
8
9
10
11


for more examples of while click me...😄😄

What is for loop..??                      What is do-while loop..??

What is switch statement..??        What is JVM..??




















No comments:

Post a Comment