Thread: true or false

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

    true or false

    Hello all

    I'm a novice both in c programming and this forum

    this is a true or false question :
    -----------------------------------------------------

    The following exrpession assigns 2 in x.

    (3 > 4 ) && ( x=2)
    -----------------------------------------------------
    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Think of how operator && behaves when its left hand operand is false. Does it go on to evaluate its right hand operand?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What laserlight is saying is . . .

    If the left side of a && expression is evaluated to false, it does not evaluate the second, right side, as it would be useless. If the left side is false, then the result is always false.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    19
    ok guys i get your point

    so what if i change it a bit :

    ( 4>3 ) && ( x=2 )

    would that be true ?

    Meaning , if the very next line of code is

    printf("%d",x)

    will i get a

    2


    on the screen ?


    Thanks for all the help

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Well, if you tried it . . .

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        int x = 0;
        (4>3) && (x = 2);
        printf("%i\n", x);
        return 0;
    }
    . . you get . . .

    Code:
    2
    So, then, yes, you are correct.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 12:48 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM