What will be the content of y and z after executing the code given below: int y=10, z=2; for (int x = 1; x <= 10; x=x+2) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What will be the content of y and z after executing the code given below: int y=10, z=2; for (int x = 1; x <= 10; x=x+2)

{ y - - ; ++z; }

2nd Aug 2020, 6:20 AM
Biswajit Sarkar
Biswajit Sarkar - avatar
3 Answers
+ 2
iteration 1: y = 10 z = 2 x = 1 and is less than or equal to 10 so, y--; ++z; so y becomes 9 and z becomes 3 and x = x + 2 = 1 + 2 = 3 iteration 2: y = 9 z = 3 x = 3 and is still less than or equal to 10 so, y--; ++z; so y becomes 8 and z becomes 4 and x = x + 2 = 3 + 2 = 5 iteration 3: y = 8 z = 4 x = 5 and is less than or equal to 10 so, y--; ++z; so y becomes 7 and z becomes 5 and x = x + 2 = 5 + 2 = 7 iteration 4: y = 7 z = 5 x = 7 and is less than or equal to 10 so, y--; ++z; so y becomes 6 and z becomes 6 and x = x + 2 = 7 + 2 = 9 iteration 5: y = 6 z = 6 x = 9 and is less than or equal to 10 so, y--; ++z; so y becomes 5 and z becomes 7 and x = x + 2 = 9 + 2 = 11 iteration 6: y = 5 z = 7 and x = 11 now the for loop block won't be executed because the for loop condition fails. (x<=10) that is 11 is not less than or equal to 10
2nd Aug 2020, 7:20 AM
Rohit
+ 1
Thank you so much
2nd Aug 2020, 7:21 AM
Biswajit Sarkar
Biswajit Sarkar - avatar
0
Hi Biswajit Sarkar. Your code snippets is incomplete.
2nd Aug 2020, 6:43 AM
Rohit