Computer Science, asked by devinair7077, 1 year ago

Distinguish between while and do-while loop with an example

Answers

Answered by iamawesome05
1

Definition of while Loop

The while loop is the most fundamental loop available in C++ and Java.  The working of a while loop is similar in both C++ and Java.The general form of while loop is:

while ( condition) {

statements;  //body of loop

}

The while loop first verifies the condition, and if the condition is true then, it iterates the loop till the condition turns out false. The condition in while loop can be any boolean expression. When expression returns any non-zero value, then the condition is “true”, and if an expression returns a zero value, the condition becomes “false”. If the condition becomes true, then loop iterates itself, and if the condition becomes false, then the control passes to the next line of the code immediately followed by the loop.

The statements or the body of the loop can either be an empty statement or a single statement or a block of statements.

Let’s discuss the working of a while loop. In the example below the code will print from 1 to 10.

// example is in Java.

class while{

public static void main ( args[] ){

int n=0;

while(n<=10){

n++;

system.out.println("n=" +n);

}

}

}

//output

n=1

n=2

n=3

n=4

n=5

n=6

n=7

n=8

n=9

n=10

Here, the initial value of ‘n’ is 0, which turns the condition in while loop true. The control then enters the body of while loop and the value of ‘n’ is incremented according to the first statement in the body of a while loop. Then the value of ‘n’ is printed and again, the control goes back to the condition of a while loop, now the value of ‘n’ is 1  which again satisfies the condition, and the body of the loop is executed again.

This continues till the condition is true, as soon as the condition become false the loop is terminated. Like for loop the while loop also first checks the condition and then execute the loop body.

Definition of do-while Loop

As in while loop, if the controlling condition becomes false in the first iteration only, then the body of the while loop is not executed at all. But the do-while loop is somewhat different from while loop. The do-while loop executes the body of the loop at least once even if the condition is false at the first attempt.

The general form of do-while is as follows.

do{

.

statements  // body of loop.

.

} while( Condition );

In a do-while loop, the body of loop occurs before the controlling condition, and the conditional statement is at the bottom of the loop. As in while loop, here also, the body of the loop can be empty as both C++ and Java allow null statements or, there can be only a single statement or, a block of statements. The condition here is also a boolean expression, which is true for all non-zero value.

In a do-while loop, the control first reaches to the statement in the body of a do-while loop. The statements in the body get executed first and then the control reaches to the condition part of the loop. The condition is verified and, if it is true, the loop is iterated again, and if the condition is false, then the control resumes to the next line immediate after the loop.

 

Let’s understand it by implementing above example in do-while.

// example is in Java.

class while{

public static void main ( args[] ){

int n=1;

do{

system.out.println("n=" +n);

n++;

}while(n<10)

}

}

//output

n=1

n=2

n=3

n=4

n=5

n=6

n=7

n=8

n=9

n=10

Here, the value of n=1 the control resumes to the body of the loop, the value of ‘n’ is printed and then its value is incremented. Then control resumes to the condition of the do-while loop; the condition is verified which turns out true for n=1, so, the loop is iterate’s again and continue till the condition become false.

Key Differences Between while and do-while Loop

The while loop checks the condition at the starting of the loop and if the condition is satisfied statement inside the loop, is executed. In do-while loop, the condition is checked after the execution of all statements in the body of the loop.

If the condition in a while loop is false not a single statement inside the loop is executed, and if the condition in ‘do-while’ loop is false then also the body of the loop is executed at least once then the condition is tested.

Conclusion:

Both while and do-while loop are the iteration statement, if we want that first, the condition should be verified, and then the statements inside the loop must execute then the while loop is used. If you want to test the termination condition at the end of the loop, then the do-while loop is used.

Answered by utkarshmishra9819
2

Answer:

"While" loop is an entry controlled iteration statement.On the other hand "do-while" is an exit control iteration statement.which means that the condition is checked after loop has been executed once.

Similar questions