I don't understand this

This is a discussion on I don't understand this within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { int n; // The loop goes while n< 9, and n ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    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;
    }
    Last edited by Dontgiveup; 03-28-2009 at 03:14 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    O! I got it, you put n=9 in the loop condition?

    = is assignement, == is comparison
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by vart View Post
    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. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    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. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Dontgiveup View Post
    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.
    Last edited by R.Stiltskin; 03-28-2009 at 03:59 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,002
    = 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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Ah OK, I understand now. Thank you all!

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Welcome to the joys of C/C++ syntax!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    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. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by BuzzBuzz View Post
    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. #15
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    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.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21