In: Computer Science
int value = 1;
do
{
if (value % 2 == 0)
cout << value << " ";
value = value + 1;
}
while (value % 7 != 0);
cout << "\n" << value;
What will be displayed?
Answer
Here is your answer, if you have any doubt please comment. i am here to help you.
Remember:
do_ while(): always executes body at once ,if condition is true or false.
So here. You will get the following output
I will give you how this output come with detailed step.
here is the execution of your program.
value=1; //initially
//check the body first iteration
1) i%2==0 here 1%2==0 which is FALSE, then value increment with 1, value=2.
2) check the condition for while loop, check (2%7!=0) which TRUE, then Inside loop, check (2%2==0)
which is TRUE, print the value of VALUE (2) then value=2+1=3.
3) check (3%7!=0) which is TRUE, then inside the loop, (3%2==0) which is FALSE, then VALUE=3+1=4
4) check (4%7!=0) which is TRUE, then inside the loop, (4%2==0) which is TRUE, then PRINT the
value of VALUE(4) then VALUE=4+1=5
5) check (5%7!=0) which is TRUE, then inside the loop, (5%2==0) which is FALSE, then VALUE=5+1=6.
6) check (6%7!=0) which is TRUE, then inside the loop, (6%2==0) which is TRUE, then PRINT the
value of VALUE(6) then VALUE=6+1=7.
7) check (7%7!=0) which is FALSE, loop will terminate.
8) print newline and print the current value of VALUE
any doubt please comment
Thanks in advance