#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Canada
    Posts
    8
    Rep Power
    0

    about {} braces in C++


    my book is going over the part of if statements so i skimmed through it and what not.... i noticed that the author doesn't use these {} in some of his examples. at the end of the chapters, one of the assignments is to compile a code and see how it was different from what you guessed would happen. here is the code:


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main()
    {
          int a = 2, b = 2, c;
               if(c = (a-b))
                    cout << "The vale of c is: ";
                    cout << c;
          system("PAUSE");
          return 0;
    }
    i saw that the error was in 'c' not being assigned by == but that is not the point. the parts

    Code:
    if(c = (a-b))
                    cout << "The vale of c is: ";
                    cout << c;
    if you change that to

    Code:
    if(c = (a-b))
    {
                    cout << "The vale of c is: ";
                    cout << c;
    }
    it will no longer compile, it says it gets confused and it quote "bails out", why?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    Hellas
    Posts
    46
    Rep Power
    14

    hmm...


    First of all the statement c=(a-b) it is not wrong because it does not check if the c is equal to a-b, but it the assignment of the abstraction a-b to c is succesfull.

    As of the missing braces, they are not necessary, if the rest of your code does not have any other condition, and of course you want it to be in the if statement.

    Now about the error you get when you insert braces, it may has to do with the system("PAUSE") statement. The reason I can think of now, is that you have already paused the application (even just for a moment) when you close the if statement with braces, so there is nothing left to pause because there is no other statement between the if and the system command. At least maybe it is somthing relative to that.
  4. #3
  5. No Profile Picture
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Location
    The Emerald City
    Posts
    289
    Rep Power
    13
    If you are running on linux (or any other *nix) change the PAUSE to sleep 1.
    system ("sleep 1");

    I think that one point of the exercise is to notice the buffered output. You ( or at least i did) expect it to print the value of 'c' (0) and then pause for a sec and then end the program. That is not infact what happens. Instead when you run it, it pauses for a second and then 'displays' the value of 'c'. It's not that the system() command is being executed first, its that the output if buffered and doesn't actually print until the buffer is full or the buffer is flushed (in this case when the program ends).

    edit:
    Also interesting is to change a from 2 to 3. The if-statement doesn't test whether the assignment was successful, its tests the value being assigned to, in this case 'c'. if c == 0 as in the first case then this is equivalent to
    if ( 0 )
    which is false so it doesn't print "The value of c is:".
    if you change a to 3 then the evaluation becomes
    if ( 1 )
    which is true so the statement IS printed. This is a danger in using assignments (among other things) as test statements.

    jaa
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Seattle WA
    Posts
    863
    Rep Power
    14
    Funny thing:
    Code:
    int a = 2, b = 2, c;
    if(c = (a-b))
        cout << "The vale of c is: ";
    cout << c;
    system("PAUSE");
    Should output:
    Code:
    0Press any key to continue...
    Since c is assigned the value of a - b, and equal to zero, the string from the if clause is never printed. The value 0 should then be printed, followed by the pause prompt. But I get the zero after the pause prompt. Weird.

    Adding the braces shouldn't cause a problem. And infact, it works on my system, albeit for the above mentioned oddity. Like already said, it might have something to do with the system() call and how it interacts with your OS.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2001
    Location
    Hellas
    Posts
    46
    Rep Power
    14

    justice41:


    about the c=(a-b) we are saying the same thing. You don't need to say it in another way. if a-b is 0 the assignment in the expression is 0 so its the same as false in an if.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Canada
    Posts
    8
    Rep Power
    0
    MJ is right in terms of the output, it just displays "push any key to continue" or whatever that is......
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Location
    Kamloops, BC, Canada
    Posts
    55
    Rep Power
    13

    Re: hmm...


    Originally posted by Alkis
    First of all the statement c=(a-b) it is not wrong because it does not check if the c is equal to a-b, but it the assignment of the abstraction a-b to c is succesfull.
    Slight correction. It doesn't check if the assignment is successful or not, but checks the result of the assignment.

    c=3-2 is a successful assignment, and the result (1) allows the statement within the if {} to be called.

    c=3-3 is also a successful assignment, but the result (0) will not allow the statement within the if {} to be called.

    You might think you are saying the same thing as justice42, but you aren't. :)
    Linux is for those who hate Windows.
    FreeBSD is for those who love UNIX.
    -------
    Have you read The Handbook yet?
    How about The FAQ?
    Have you searched the mailing lists?
    Or read any of the man pages?
    Have you searched the web for BSD resources?
    In short, have you done your homework yet?

IMN logo majestic logo threadwatch logo seochat tools logo