Learning course: Learn C++
Problem Link: CodeChef: Practical coding for everyone
Feedback
the answer to this problem should be 2 in my opinion but the answer they have provided is 3. please help.
Learning course: Learn C++
Problem Link: CodeChef: Practical coding for everyone
the answer to this problem should be 2 in my opinion but the answer they have provided is 3. please help.
The provided answer, 3, is correct. As in a program, all code is executed sequentially.
In the following code block;
for (int i = 0 ; i <= 5 ; i = i + 2) {
cout << "C++"<< endl;
if(i == 4)
break;
}
Started the loop, C++ is printed. Now, we check the if condition. The condition is not satisfied, code proceeds.
Again enter the loop, C++ is printed. Check the if condition, still False. Proceeds.
Again we enter the loop. C++ is printed. Check the condition, its satisfied, break from the for loop.
for (int i = 0 ; i <= 5 ; i = i + 2) {
if(i == 4)
break;
cout << "C++"<< endl;
}
then, the answer would have been 2, as the control would have broken from the for loop before cout could have been executed.