# Thread: I don't understand this

1. ## I don't understand this

Code:
```#include <iostream>

using namespace std;

int main()
{
int n;
// The loop goes while n< 9, and n increases by one every loop, right?
for (n=0; n<9; n++){
//Now here is what I don't understand
//  What I don't understand is if I place n=9, the loop runs endlessly, why?
//  I thought by placing n=9, I was telling the program to add by one from one
//up to 9 and when it reaches 9, it should stop. Instead it gives me endless 9s!
cout<< n <<endl;
}
return 0;
}```

2. If you replace every n with an x, then it will work in exactly the same way. If you forget to replace them all, but only replace some, then what you get will be broken.

3. Originally Posted by Dontgiveup
I thought by placing n=9, I was telling the program to add by one from on upto 9 and when it reaches 9, it should stop. Instead it gives me endless 9s!
I too would expect that the loop would terminate if you assign 9 to n in the loop body. Perhaps you should show the actual program that demonstrates this infinite loop.

4. I do not understand your question

Code:
```#include <iostream>

using namespace std;

int main()
{
int n;
// The loop goes while n< 9, and n increases by one every loop, right?
for (n=0; n<9; n++){
//Now here is what I don't understand
//  What I don't understand is if I place n=9, the loop runs endlessly, why?
//  I thought by placing n=9, I was telling the program to add by one from on up to 9 and when it reaches 9, it should stop. Instead it gives me endless 9s!
n=9;
cout<< n <<endl;
}
return 0;
}```
runs once for me and exits the loop

5. O! I got it, you put n=9 in the loop condition?

= is assignement, == is comparison

6. Originally Posted by vart
O! I got it, you put n=9 in the loop condition?

= is assignement, == is comparison
Still, it is the same for me. When I put n=9, I get endless 9s. When I put n==9, I get just a blank screen. I really do not understand it. I mean what is the difference between n<9, n<=9 and n=9? The first two mean n is less than 9 and n is less or equal to 9 and tell the loop to run until n is less than or equal to 9. Now what about the last one? I thought n=9 means the loop should run until 9 is equal to 9 just the way n<9 means the loop should run until n is less than 9 i.e. 8?

7. Just to make things clear. The code I posted is FINE and has no problem and delivers the result required. My question was if I replace the n<9 in the code with n=9, I get endless 9s. Why because my understanding was that n=9 means the loop should run till n=9?

8. Originally Posted by Dontgiveup
Just to make things clear. The code I posted is FINE and has no problem and delivers the result required. My question was if I replace the n<9 in the code with n=9, I get endless 9s. Why because my understanding was that n=9 means the loop should run till n=9?
If you replace the n<9 with n=9, then on every iteration you are not testing anything. You have replaced the test "is n less than 9?" with an assignment:"set n equal to 9".

First the n++ term increments n to 10, then the n=9 term reassigns 9 to n, and then you execute the code inside the loop, printing 9. Over and over again infinitely.

Edit: maybe what you wanted was to replace the n<9 with n==9. That would be a test of "is n equal to 9?" and, if you initialized n to 9, would cause the loop to execute exactly once, because on the second iteration n would have been incremented to 10 so would fail this test. If you initialized n to 0 the code inside the loop would never be executed.

9. Because your understanding is grossly mistaken. The for loop runs just so long as the middle condition is true. n=9 is always true, hence your loop doesn't stop. (The reason it is always true is that, as pointed out above, n=9 is not a comparison but an assignment.) If you had done n==9, the loop wouldn't run at all, since n does not start out as 9, so the condition starts out false, hence nothing happens.

10. = is assign, ie: n = 9 -> Set n to the number 9.
== is compare, ie: n == 9 -> Tests is n is 9; returns true if it is, false if not.
The loop condition requires a boolean expression, ie something that evaluates to true or false, like a comparison, or you will get strange results.

11. Ah OK, I understand now. Thank you all!

12. Welcome to the joys of C/C++ syntax!

13. When writing code to help with the syntax I say it out loud in my head (that makes sense to me) with "=" being "is" and "==" being "equal to".

x = 3;
x "is" 3

x == 3;
x "equal to" 3

The = sign(s) also correlate with the amount of words used. I find talking out my code helps me clarify it (especially with conditional operators), it does also make me look a bit like a mental case to the casual observer.

14. Originally Posted by BuzzBuzz
When writing code to help with the syntax I say it out loud in my head (that makes sense to me) with "=" being "is" and "==" being "equal to".

x = 3;
x "is" 3

x == 3;
x "equal to" 3
I like to use
Code:
```x = 3; //x gets three
x == 3; //x is three ```

15. It's all horses for courses. Whatever works best for you is best for you. I prefer to think of "=" as "is" as you are assigning that value to variable as opposed to it being the same as the other.

Code:
```if (chalk == cheese)
branston = pickle;```
Sandwich worries solved.