1. The conditon of a "while" loop and a "for" loop are executed at the ____________ of these loops.
2. What will the following code fragment print?
for (i = 0; i < 4; i++)
cout << i + 1;
cout << i;
3. What is the result in answer below?
int int1=16;
int int2=5;
int answer=0;
answer = int1 / int2 + (5 * 2);
1) Beginning/ start
2) 12344
3) 13
========================================================
Explanation:
1) The for loop and while loop both of them check the condition and then enter the loop. This is different from do-while loop where condition is checked at end of loop.
2) Initially i is 0, so 0+1 = 1 is printed.
In next iteration i is 1 so 2 is printed.
In next iteration i is 2 so 3 is printed.
In next iteration i is 3 so 4 is printed.
In next iteration i is 4 but the loop condition is i<4, so loop breaks.
Outside loop again i i.e. 4 is printed.
So output = 12344
3) answer = int1/int2 + (5 * 2);
answer = 16/5 + (5 * 2);
5*2 is 10.
Since it is integer operation only ( operands are integer) , 16/5 = 3
So answer = 3 + 10 = 13
So the result in answer is 13.
====================================
Please upvote.
Get Answers For Free
Most questions answered within 1 hours.