In: Computer Science
In pseudocode, consider the DOWHILE repetition control structure. Control passes to the next statement after the delimeter ENDDO (and no further processing takes place within the loop) when which of the following occurs? a condition p is found to not evaluate to true or false a condition
p is found to be true
a condition p is found to null
None of the other answers are correct
a condition p is found to be false
ans) a condition p is found to be false
explaination:
Like while the do-while loop execution is also terminated on the basis of a test condition. The main difference between a do-while loop and while loop is in the do-while loop the condition is tested at the end of the loop body, i.e do-while loop is exit controlled whereas the other two loops are entry controlled loops.
Syntax:
do
{
// loop body
update_expression;
}
while (test_expression);
The various parts of the do-while loop are:
i <= 10
i++;
How does a do-While loop executes?
Flow Diagram do-while loop:
// C++ program to illustrate do-while loop
#include <iostream>
using namespace std;
int main()
{
// Initialization expression
int i = 2;
do {
// Loop body
cout << "Hello World\n";
// Update expression
i++;
}
// Test expression
while (i < 1);
return 0;
}
Output:
Hello World
Dry-Running Example 1:
1. Program starts. 2. i is intialised to 2. 3. Execution enters the loop 3.a) "Hello World" gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 < 2 yields false. 5. The flow goes outside the loop.
// C program to illustrate do-while loop
#include <stdio.h>
int main()
{
// Initialization expression
int i = 1;
do {
// Loop body
printf("%d\n", i);
// Update expression
i++;
}
// Test expression
while (i <= 5);
return 0;
}
Output:
1 2 3 4 5