difference between while loop and do while loop with an example from Java
Answers
Answered by
5
The difference between while loop and do while loop is that while loop is an entry controlled loop and do while is an exit controlled loop. This means that while loop checks the condition BEFORE executing commands in contrast to do while loop which checks the condition AFTER the commands
int i=0;
while(i!=0)
{
System.out.println("Hello");
i++;
}
The loop will not get printed because the condition is false.
int i=0;
do
{
System.out.println("Hello");
i++;
}while(i!=0);
The loop will get executed once.
int i=0;
while(i!=0)
{
System.out.println("Hello");
i++;
}
The loop will not get printed because the condition is false.
int i=0;
do
{
System.out.println("Hello");
i++;
}while(i!=0);
The loop will get executed once.
Similar questions