Operator |
Meaning |
|---|---|
A > B |
True ( 1 ) if A is greater than B, false ( 0 ) otherwise |
A < B |
True ( 1 ) if A is less than B, false ( 0 ) otherwise |
A >= B |
True ( 1 ) if A is greater than or equal to B, false ( 0 ) otherwise |
A <= B |
True ( 1 ) if A is less than or equal to B, false ( 0 ) otherwise |
A = = B |
True ( 1 ) if A is exactly equal to B, false ( 0 ) otherwise |
A != B |
True ( 1 ) if A is not equal to B, false ( 0 ) otherwise |
Operator |
Meaning |
|---|---|
A && B |
A AND B - True ( 1 ) if A is true AND B is also true, false ( 0 ) otherwise |
A | | B |
A OR B - True ( 1 ) if A is true OR B is true, false ( 0 ) otherwise |
! A |
NOT A - True ( 1 ) if A is false ( 0 ), false ( 0 ) if A is true ( non-zero ). |
int year; bool leap_year; leap_year = ( ( ( ( year % 4 ) == 0 ) && ( ( year % 100 ) != 0 ) ) || ( ( year % 400 ) == 0 ) ); // Or equivalently: leap_year = !( year % 4 ) && year % 100 || !( year % 400 );
if( condition )
true_block
else
false_block
if( x < 0.0 ) {
printf( "Error - The x coordinate cannot be negative. ( x = %f ) \n", x );
exit( -1 );
}


if( x < 0.0 )
printf( "Error - The x coordinate cannot be negative. ( x = %f ) \n", x );
exit( -1 );
const int left = 1, right = 2, up = 3, down = 4; // For improved readability
int direction;
. . .
printf( "Please enter a direction, 1 for left, 2 for right, ... " );
scanf( "%d", &direction );
if( direction = = left )
x--;
else if ( direction = = right )
x++;
else if ( direction = = up )
y++;
else if ( direction = = down )
y--;
else
printf( "Error - Invalid direction entered.\n" );
const int left = 1, right = 2, up = 3, down = 4; // For improved readability int direction; . . .printf( "Please enter a direction, 1 for left, 2 for right, ... " ); scanf( "%d", &direction ); switch( direction ) { case left: x--; break; case right: x++; break; case up: y++; break; case down: y--; break; default: printf( "Error - Invalid direction entered.\n" ); break; } /* End of switch on direction */
switch( variable_integer_expression ) {
case constant_integer_expression_1 :
code block 1;
break; // ( Optional - See below )
// Repeat the case sub-construct as many times as needed
default: // Optional
default code block;
break; // Not needed. Added for defensive programming only.
} // End of switch on . . .
bool done = false; // Assumes stdbool.h has been #included
char choice;
while ( ! done ) {
// Code to print out a menu and ask for choice omitted
scanf( "%c", &choice );
switch( choice ) {
case 'E':
case 'e':
/* Code here to handle Entering data */
break;
case 'C':
case 'c':
/* Code here to handle Calculations */
break;
case 'H':
case 'h':
case '?':
/* Code here to deliver Help */
break;
case 'Q':
case 'q':
done = true;
break;
default:
printf( "Error - Invalid choice: %c\n", choice );
printf( "Choose 'H' for help.\n";
break;
} /* End of switch on menu choices */
} /* End of infinite while loop */
Earlier we looked at unary and binary operators under C. In addition, the C language contains one ternary operator, known as the conditional operator, ?:
condition ? true_clause : false_clause
max = i > j ? i : j; abs = x > 0 ? x : -x; printf( "X is %s.\n", ( x < 0 ? "negative" : "non-negative" ) );
// Parentheses needed in the last example because + and * higher than > and ?: pay = base + ( sales > quota ? 1.5 : 1.0 ) * bonus;
if( error < tolerance )
step *= 2.0;
else
step = 1.0;
if( x != 0.0 )
z /= x;
else
z = infinite;
if( x > 0.0 )
sign = 1.0;
else if ( x = = 0.0 )
sign = 0.0;
else sign = -1.0;
The following topics are not covered here, but may be found in many books on C/C++;