Why does this ==20 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why does this ==20

#include<iostream> using namespace std; int main() { int x, y, z; y = z = 0; for (x=0; x<10; x++) { y = y + 1; } z = x + y + z; cout << z; }

7th Jul 2020, 10:38 PM
Y0gurt
Y0gurt - avatar
2 Answers
+ 3
Y0gurt because you have both x & y adding 1 each cycle x++ is incrementing and y = y + 1 is doing the same thing so when x is 9 it increments one last time to 10 and y = 10 so x + y = 20 + z which is 0 as it was not used in the loop itself. So when z was finally used it was 20
7th Jul 2020, 11:02 PM
BroFar
BroFar - avatar
+ 1
BroFar BroFar thank you so much! that helps alot :)
8th Jul 2020, 1:12 AM
Y0gurt
Y0gurt - avatar