What is the final value of y? //Program #include<iostream> using namespace std; int main() { int y; for(y=2;y<=6;y+=2) cout<<y; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the final value of y? //Program #include<iostream> using namespace std; int main() { int y; for(y=2;y<=6;y+=2) cout<<y; }

14th Sep 2016, 2:14 PM
Venus
Venus - avatar
5 Answers
+ 4
No, the increment is done at each loop except the first. Besides, if you look at the condition, you know that y has to be greater than 6 for the loop to end. Here is the equivalent with a while loop: int y = 2; while (y <= 6) { cout << y; y += 2; }
14th Sep 2016, 2:59 PM
Zen
Zen - avatar
+ 2
Okay. So it means that the value which will get printed will be 6, but the final value of y will be 8? And Thanks!!
14th Sep 2016, 3:09 PM
Venus
Venus - avatar
+ 1
There is no spoon, I mean, no x. If you mean y, the program will print 246 and the value of y will be 8 when the for loop ends.
14th Sep 2016, 2:21 PM
Zen
Zen - avatar
+ 1
That's right.
14th Sep 2016, 4:38 PM
Zen
Zen - avatar
0
Shouldn't it be 6?? As it will be the last time the loop runs....
14th Sep 2016, 2:47 PM
Venus
Venus - avatar