Thread: If (statement) and (statement)

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    11

    If (statement) and (statement)

    Hi, how to have muilti-conditions on an IF statment?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    if (a == b && c == d)
    {
     // true
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    thanks, this board ROCKS! Im almost done my first c++ program, TIC TAC TOE!

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    hm,, why this dont work
    Code:
    if (playerturn % 2 == 1 && boardarray2[moverow][movecollumn]=false)

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    For the sake of an example, let's deal with expressions with integer values:

    In C, expressions that evaluate to non-zero values are considered "True" when participating in logical operations. Expressions that evaluate to zero are considered "False".

    Here's the example using the && logical operator:

    Code:
    if (first_stuff && second_stuff) {
    
       // This is executed if both are true
    
    }
    
    else {
    
       // This is excuted if neither is true
    
    }
    Here's how it goes: first_stuff is evaluated. If it has a zero value, control passes to the else block. If first_stuff has a non-zero value, second_stuff gets evaluated. If this has a zero value, control passes to the else block. The only way that the first block in this example ever gets executed is whenever both first_stuff and second_stuff are non-zero (true).

    Dave

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Code:
    if (playerturn % 2 == 1 && boardarray2[moverow][movecollumn]=false)
    = should be ==.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>boardarray2[moverow][movecollumn]=false)
    How many equals sign are needed to test two variables? Two. One is an assignment, not a comparison.

    [edit]
    Doh! beat.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    But isn't it: if( true )? That means instead of == false you could also do: if( !condition ). ! meaning not, and that being true, the same thing as false. Right?

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Right?
    Right (since we are talking about a boolean value). If you were talking about a general test against 0 then I would probably disagree on the grounds that using (!condition) when (condition == 0) would be more sensible results in obscure code. But if the condition being tested really is boolean in nature, (!condition) is usually more clear.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM