How to use three && in C programming?

This is a discussion on How to use three && in C programming? within the C Programming forums, part of the General Programming Boards category; Code: If (a[q]==a[i+1] && b[q]==b[i+1] && c[q]==c[i+1]){ ................................ } Can i do above written statement in C? I tried but ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    55

    How to use three && in C programming?

    Code:
    If (a[q]==a[i+1] && b[q]==b[i+1] && c[q]==c[i+1]){
                            ................................
    }
    Can i do above written statement in C? I tried but it didn't work.

    Sorry tabstop i typed here in correctly. In my real program I was missing one = sign in c[q].

    Anyway thanx for help.
    Last edited by curious; 06-05-2008 at 11:32 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Nothing wrong with the &&. There's a lot of things wrong with "=", though.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    You need to use == to make comparison. = is the assignment operator.
    and 'if' is lower case

  4. #4
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    And make sure you are aware of what you're checking. Making yourself a binary table is helpful:
    1 && 1 && 1 = 1
    1 && 1 && 0 = 0
    1 && 0 && 1 = 0
    1 && 0 && 0 = 0
    0 && 1 && 1 = 0
    0 && 1 && 0 = 0
    0 && 0 && 1 = 0
    0 && 0 && 0 = 0

    (I know it's silly. ANYONE who's been programming of any length of time could have told you that, but it's a good exercise to be able to do for more complex examples.) As the table above shows you're ONLY going to pass (once you fix that little == problem) when ALL the conditions are true.

    For some reason I feel this is a part of a larger program that will be testing more than this case. it may be useful for you to remember that in C/C++ true and false are really just 1 and 0, the sort you can do math with. Change the original line to:
    Code:
    int sum;
    sum = (a[q]==a[i+1]) + (b[q]==b[i+1]) + (c[q]==c[i+1]);
    and sum will hold the number of arrays where [q]==[i+1]. Or change it to:
    Code:
    int bsum;
    bsum = 1*(a[q]==a[i+1]) + 2*(b[q]==b[i+1]) + 4*(c[q]==c[i+1]);
    And you will have a unique number for each possible case based on binary numbers, IE 0 means no pass (000), 1 means the first passed (001), 2 means the second passed (010), 3 means the first and second passed (011), etc.

    Lots of possibilities.

    But then again you may only want the case you showed us.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. str_replace && str_find
    By q6z4k in forum C Programming
    Replies: 12
    Last Post: 06-01-2008, 05:03 PM
  2. problem w/ color functions && win98 :P
    By DarkMortar in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 05:45 PM
  3. AnimateWindow && Dev-C++ problems
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 03-13-2006, 04:34 PM
  4. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 08:19 AM
  5. && or ||
    By chrismax2 in forum C++ Programming
    Replies: 4
    Last Post: 08-17-2005, 05:42 PM

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