|
if ( boolean expression ) one-statement // Then Body |
Otherwise the statement in the then body is SKIPPED.
if ( boolean expression ) one-statement // Then Body else one-statement // Else Body |
Otherwise the statement in the ELSE body is executed.
Examples:
x > y x*x + y*y <= z*z |
Operate (compares) on numerical values and returns a Boolean (logical) result value
Relational Operator | Description |
---|---|
> | Greater than |
>= | Greater than or equal |
< | Less than |
<= | Less than or equal |
== | Equal |
!= | Not equal |
Operate (compares) on Boolean (logical) values and returns a new Boolean (logical) result value
Logical Operator | Description |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
if ( x < 0 ) x = -x; |
if ( (x >= 10 && x < 20) ) ... else ... |
if ( !(x >= 10 && x < 20) ) ... else ... which is equivalent to: if ( x < 10 || x >= 20 ) ... else ... |
if ( x < 10 ) { // Fact: x < 10 if ( x > 2 ) { ... executed when x < 10 and x > 2 } else { ... executed when (x < 10 and) x <= 2 } } else { // Fact: x >= 10 if ( x > 17 ) { ... executed when (x >= 10 and) x > 17 } else { ... executed when x < 10 and x <= 17 } } |
if ( avg >= 90 ) { printf("Grade is A"); // x >= 90 } else { // Fact: x < 90 if ( avg >= 80 ) { printf("Grade is B"); // Fact: x in [80,90) } else { // Fact: x < 80 if ( avg >= 70 ) { printf("Grade is C"); } else { if ( avg >= 60 ) { printf("Grade is D"); } else { printf("Grade is F"); } } } } |
if ( avg >= 90 ) { printf("Grade is A"); } else if ( avg >= 80 ) { printf("Grade is B"); } else if ( avg >= 70 ) { printf("Grade is C"); } else if ( avg >= 60 ) { printf("Grade is D"); } else { printf("Grade is F"); } |
switch ( EXPRESSION ) { case CONSTANT1: one or more statements; break; case CONSTANT2: one or more statements; break; ... [default: one or more statements;] } |
switch ( Avg ) { case 90: case 91: ... case 100: printf("Grade is A"); break; case 80: case 81: ... case 89: printf("Grade is B"); break; case 70: case 71: ... case 79: printf("Grade is C"); break; case 60: case 61: ... case 69: printf("Grade is D"); break; default: printf("Grade is F"); } |