Printing different messages depending on the value of a variable in a for loop

This is a discussion on Printing different messages depending on the value of a variable in a for loop within the C++ Programming forums, part of the General Programming Boards category; Beginner here so bear with me. This is what I have so far: Code: #include <iostream> using namespace std; int ...

  1. #1
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37

    Printing different messages depending on the value of a variable in a for loop

    Beginner here so bear with me. This is what I have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x = 0;
        
        if (x == 1000) {
             cout<<"X is finally equal to 1000.\n";
        }
        else {
             for (; x <= 1000; x++)
        {
            cout<<"X is equal to " << x << ".\n";
        }
    }
        cin.ignore();
    }
    What I want this to do is increment x by 1 repeatedly and print the current value of x at any given time until it reaches 1000, in which case it should add the word "finally". When I compile and run this code it increments x all the way up to 1000 but when it reaches 1000 it simply prints "X is equal to 1000.", ignoring the condition of the if statement that encloses it.

    I think the problem is that the for loop wants to increment x until x is less than or equal to 1000, but at that point I want the if function to take over, and print its message. I'm not really sure how to go about doing that.

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Put the if() inside the for()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    Quote Originally Posted by Salem View Post
    Put the if() inside the for()
    Thanks. Could you explain exactly why it has to be in that order? Because to me the most logical thing to do is check whether x is equal to 1000 and if it's not, increment it.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Because you can't just list things you need to do in any random order.

    You put the "if x is 1000" first, before the loop executes. So you get exactly 1 shot at that being true, and then the code moves on to execute the for loop, never again to compare x with 1000.

    Try this
    Code:
    for ( x = 0 ; x <= 1000 ; x++ ) {
      if ( x == 1000 ) {
        cout<<"X is finally equal to 1000.\n";
      } else {
        cout<<"X is equal to " << x << ".\n";
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User ex-mortis's Avatar
    Join Date
    Mar 2012
    Posts
    37
    Quote Originally Posted by Salem View Post
    Because you can't just list things you need to do in any random order.

    You put the "if x is 1000" first, before the loop executes. So you get exactly 1 shot at that being true, and then the code moves on to execute the for loop, never again to compare x with 1000.

    Try this
    Code:
    for ( x = 0 ; x <= 1000 ; x++ ) {
      if ( x == 1000 ) {
        cout<<"X is finally equal to 1000.\n";
      } else {
        cout<<"X is equal to " << x << ".\n";
      }
    }
    Makes sense. Thanks a lot for your help.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by ex-mortis View Post
    Thanks. Could you explain exactly why it has to be in that order? Because to me the most logical thing to do is check whether x is equal to 1000 and if it's not, increment it.
    Because a program is executed sequentially. Your if statement is executed before the loop (when x == 0).
    The loop compromises all of the statements in the enclosing braces of the for (that is, the if is outside the loop!).
    Btw, you should indent properly.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-21-2012, 04:51 PM
  2. Bidirectional loop? (Increase/decrease depending on values)
    By BlueGooGames in forum C Programming
    Replies: 4
    Last Post: 01-08-2011, 11:08 AM
  3. reading from a file depending on variable
    By zidsal in forum C++ Programming
    Replies: 16
    Last Post: 06-13-2008, 03:55 PM
  4. Printing a variable's name.
    By BrokenShots in forum C Programming
    Replies: 9
    Last Post: 03-12-2008, 03:13 AM
  5. Replies: 18
    Last Post: 12-31-2005, 12:56 PM

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