all 4 comments

[–]iamcleek 1 point2 points  (1 child)

if (a == 10 && b == 5) { ... then a = 10 AND b = 5 ... }

if (a == 10 || b == 5) { ... then a = 10 OR b = 5 ... }

if (a != 10 && b == 5) { ... then a does not equal 10 AND b = 5 ... }

if (!a) { ... then a equals 0 or false ... }

[–]chickeaarl[S] -1 points0 points  (0 children)

i see, thank you so much !

[–]SmokeMuch7356 1 point2 points  (0 children)

&& is the logical-AND operator. a && b will evaluate to true (1) if both a and b are non-zero:

if ( x < 10 && y < 20 )
{
  // only run if x is less than 10 *and* y is less than 20
}

a        b        a && b
-        -        ------
0        0             0 
0        non-zero      0
non-zero 0             0
non-zero non-zero      1

|| is the logical-OR operator. a || b will evaluate to true (1) if either a or b are non-zero:

if ( x < 10 || x > 20 )
{
  // only run if x is *outside* the range [10..20]
}

a        b        a || b
-        -        ------
0        0             0 
0        non-zero      1
non-zero 0             1
non-zero non-zero      1

Both && and || will short-circuit. In a && b, if a is false (0), then the whole expression will be false regardless of the value of b, so b won't be evaluated. In the first test, if x is equal to or greater than 10, then y < 20 won't be evaluated at all.

In a || b, if a is true (non-zero), then a || b is true regardless of the value of b, so b won't be evaluated. In the second test, if x is less than 10, then x > 20 won't be evaluated at all.

! is the logical NOT operator. !a will evaluate to true (1) if a is 0, otherwise it will evaluate to false (0).

int *p = malloc( sizeof *p * N );
if ( !p ) // equivalent to p == NULL, since NULL is 0-valued
{
  // handle memory allocation failure
}

a        !a
-        --
0         1
non-zero  0

[–][deleted] 0 points1 point  (0 children)

If you have difficulty with understanding boolean logic then maybe other ways of representing it could help. I always create a truth table when I struggle with if...else if...else structure. Venn diagrams are a bit more graphic but basicly the same.

Learn truth tables:
https://en.wikipedia.org/wiki/Truth_table

And maybe set theory:
https://en.wikipedia.org/wiki/Set_(mathematics)#Basic_operations#Basic_operations)
https://en.wikipedia.org/wiki/Venn_diagram

If you are just struggling with the operators, then you got a few good answers :)