In what sequence the initialization, testing and execution of body is done in a do-while loop?
Make a short program to justify your selection.
a: Initialization, execution of body, testing
b: Execution of body, initialization, testing
c: Initialization, testing, execution of body
d: None of the above
Answers
Answered by
1
a: Initialization, execution of body, testing
let counter = 5
do {
counter++
console.log("I will run once without testing for condition")
}
while (counter < 5);
Answered by
0
In do-while loop the execution of the body happens before the testing process. So option 'a' is the correct answer.
Here is an C++ example of the do-while loop that justifies its execution process:
#include<iostream>
using namespace std;
int main()
{
//initialization
int n=5;
do{
//execution of the body
cout<<" The Answer is:" <<" " <<n;
}
//testing of the condition
while(n>5);
return 0;
}
Output: The Answer is: 5
Here, the condition is false, but the control or flow of the program goes like: initialization, execution, and then the testing.
In do- while loop condition is checked at the at end of the loop. It means even if the condition is false the statement written inside the body of loop will be executing at least for once.
Similar questions