# 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. ## 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.

2. Nothing wrong with the &&. There's a lot of things wrong with "=", though.

3. You need to use == to make comparison. = is the assignment operator.
and 'if' is lower case

4. 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.

Popular pages Recent additions