Like Tree1Likes
  • 1 Post By grumpy

can someone explain if statements in c++

This is a discussion on can someone explain if statements in c++ within the C++ Programming forums, part of the General Programming Boards category; Am I suppose to use curly braces in if statements In my textbook it shows if statements without curly braces ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    can someone explain if statements in c++

    Am I suppose to use curly braces in if statements

    In my textbook it shows if statements without curly braces

    like this
    Code:
    if(x=0)
    cout<< x << "is 0" <<endl;
    or is it suppose to be like this

    Code:
    if(x=0){
    cout<< x << "is 0" <<endl;
    }

    or is there a difference between both because my professor gave me a sample code and he had curly braces in there

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    The examples, as you have shown them, are equivalent.

    The difference shows up if you have more than two statements.
    Code:
    if (x == 0)
        step_1();
        step_2();
    will always execute step_2() regardless of the value of x (indentation of the code doesn't change that) whereas
    Code:
    if (x == 0)
    {
        step_1();
        step_2();
    }
    will not.

    Also, be careful of
    Code:
    if (x = 0)
         cout<< x << "is 0" <<endl;
    as it does NOT test if x is zero. It assigns x to the value zero, and then does not produce any output. The equality test is == (two equal signs, not one).
    rogster001 likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    14
    so let me get this clear

    Code:
    if (x == 0)
       cout << "Run this program"<<endl;
        cout <<"Run this too"<<endl;
    will always execute step_2() regardless of the value of x (indentation of the code doesn't change that) whereas
    so it will display the statement "Run this too" if x is 1 and not 0
    and both if x is 0. So basically step 2 in this one has nothing to do with the if statement before it?





    Code:
    if (x == 0)
    {
        cout << "Run this program"<<endl;
        cout <<"Run this too"<<endl;
    }
    and this code will display both statements only if x = 0.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,588
    Quote Originally Posted by c++noob145 View Post
    and both if x is 0. So basically step 2 in this one has nothing to do with the if statement before it?

    Code:
    if (x == 0)
    {
        cout << "Run this program"<<endl;
        cout <<"Run this too"<<endl;
    }
    and this code will display both statements only if x = 0.
    exactly right. the compiler considers the first statement after the if to be the one controlled by it. a block of statements wrapped in curly brackets will be seen by the compiler as one statement, with respect to the if statement.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    The curly braces give the ' scope ' of the IF statement - the number of instructions it encapsulates
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Quote Originally Posted by rogster001 View Post
    The curly braces give the ' scope ' of the IF statement - the number of instructions it encapsulates
    No it doesn't. The scope of an if statement is something else entirely.

    The curly braces are used to compose a single statement from multiple statements.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain to me
    By roelof in forum C++ Programming
    Replies: 11
    Last Post: 06-20-2010, 04:56 AM
  2. no one is able to explain!
    By c_lady in forum C Programming
    Replies: 3
    Last Post: 05-12-2010, 10:47 AM
  3. Someone explain this to me
    By bigmac(rexdale) in forum C Programming
    Replies: 5
    Last Post: 10-12-2008, 04:29 PM
  4. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 06:23 AM
  5. plz explain
    By polonyman in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 05:15 AM

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