26 Jan 2017

What is do-while loop..??

This loop is used when there is a need to repeatedly execute a group of statements as long as condition is true.If the condition is false the repetition will be stopped and the execution  comes out of the loop.

So the syntax of do-while loop :

do
{
statements;
}while(condition);

Lets take an example of

class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x<=10);
}}

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

while loop is also called as entry restricted ,similarly the do-while loop is also know as entry level loop.

The question arises what is the differences  between while and do-while loop..??

The difference is that in do while loop , the loop must execute at first time if the condition false or true but in while loop if the condition is false then the JVM comes out to the loop ..


Lets take as example of do-while loop in which the while condition is false :


class mobile
{public static void main(String ar[])

{int x=1;
do{System.out.println(x);
x++;
}
while(x>=10);
}}

When you execute this program ,the loop will execute first time but after that it checks the condition which is false by me so the loop will exit.


 for more examples of do-while loop  click me .....😄😄


What is while loop ..??           What is for loop..??

What is switch statement..??   What is control statements..??











No comments:

Post a Comment