0
Thanks but the question is why is it an infinite loopđ. to be more specific it starts the values from 50
3 Answers
+ 5
Okay.
The line:for (int a = 0; a = 50; a+=10)
makes it an infinite loop.
because here,
In the first time, a becomes 0.Then when it goes to the condition(second perameter) a becomes 50 and the condition returns 50 which makes it an infinite loop.
Now it prints the value of a which it 50 .
In the second time,
a becomes 60,as a=a+10 but when it again goes to the condition a becomes 50 and return the condition 50 like before and the loop continues .
Then it again prints the value of a which is 50 and it goes on and on.
+ 5
Use "a <= 50" rather than "a = 50" .
Emmanuel Abraham
EDITED answer to your edited QUESTION:
Because in the beginning of the loop, the value of a is 0 then you add 10 to it repeatedly until it reached 50, Then when it became 50 it meets the condition "a = 50", it always match the condition and prints the output (50) and results to an infinite loop.
The reason why 10,20,30,40 were not printed is because they don't match the condition.
In other words, these numbers are not equal to 50 and when the condition is False, the code below will not be executed
https://code.sololearn.com/cQR5q16tEgm6/?ref=app
+ 5
int a = 0 runs once
Condition: a = 50
"=" Is the assignment operator. So "a" is 50 now.
Assignments return the right-hand operand so the condition is 50 (yes the number alone).
Is 50 a true or false value? It's true (as almost everything different than zero as far as I know)
So the loop runs because the condition is true.
cout << a; // 50
Now add 10 to "a" and it becomes 60
The condition runs again. a = 50 now. The condition (50) is true so the loop runs again.
cout << a; // 50
Now add 10 to "a" and it becomes 60
The condition runs again...
I hope I made myself clear