Sorry if this has been asked before

This is a discussion on Sorry if this has been asked before within the C++ Programming forums, part of the General Programming Boards category; Code: int x = 0; if (x == 0) { x = 5; ...*lot's more code*... //why does this stuff ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    249

    Sorry if this has been asked before

    Code:
    int x = 0;
    
    if (x == 0)
    {
         x = 5;
        ...*lot's more code*...      //why does this stuff execute?
    }
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    because an if statement executes everything in the block following it if the condition is true. The comparison is made before x is given a new value.
    Away.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Ok, that's what I thought.

  4. #4
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    That will execute until x is given a new value before the statement, like

    Code:
    x = 10;
    Now, if u didnt want it to execute you could aso edit the if condition as such:

    Code:
    if (x != 0){}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-17-2005, 03:52 AM
  2. i asked MS
    By itld in forum Linux Programming
    Replies: 2
    Last Post: 04-23-2002, 07:50 AM
  3. Replies: 2
    Last Post: 03-01-2002, 08:47 AM
  4. Replies: 1
    Last Post: 11-01-2001, 05:59 PM

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