if( condition ) { BB1; } else { BB2; } |
![]() |
if (x < 0.0) { printf("x is negative.\n"); } else { printf("x is non-negative.\n"); } |
![]() |
If the condition is true, the BB1 will execute and then continue with the code after the close curly brace. If the condition is false, it will skip over BB1 and go to BB2. Once it completes BB2, it will continue with the code after the curly brace.
When you want to choose between two actions.
One way to choose between three choices is a nested if-else statement. Imagine instead of negative and non-negative, we want to identify negative, zero, and non-negative. With nested if statements, we do not view it as choosing between three - we first choose between two categories (e.g. positive and non-positive) and then separately distinguish between one of the others (non-positive is split between zero and negative).
if and else basic blocks (BB) can contain any arbitrarily complex code - it could include loops, sequential code intermixed with code, etc. Once you are inside the BB, the program can be anything.
if( condition1 ) { BB1; } else { if (condition2) { BB2; } else { BB3; } } |
![]() |
if (x < 0.0) { printf("x is negative.\n"); } else { if (x > 0.0) { printf("x is positive.\n"); } else { printf("x is zero.\n"); } } |
![]() |
If condition1 is true, the BB1 will execute and then continue with the code after the close curly brace. If condition1 is false, it will skip over BB1 and go to the else clause. The else clause contains more code, including another if statement. It will then evaluate the condition2. If condition2 is true, then it will run BB2. If condition2 is false, it will run BB3. Once it finishes, it will continue with code after the last curly brace.
The if statement could contain anything - not just a nested if statement. There can
be loops, function calls, etc.
When to use this construct:
When you do not have a simple case. You want to choose between two things, but then you have further subdivisions of the task.
if( condition1 ) { BB1; } else if (condition2) { BB2; } else if (condition3) { BB3; } else // doesn't always need an else { BB4; } |
![]() |
if (x < 0.0) { printf("x is negative.\n"); } else if (x > 0.0) { printf("x is positive.\n"); } else { printf("x is zero.\n"); } |
![]() |
If condition1 is true, the BB1 will execute and then continue with the code after the close curly brace. If condition1 is false, it will skip over BB1 and go to the next condition. It will then evaluate the condition2. If condition2 is true, then it will run BB2. If condition2 is false, it will run BB3. Once it finishes, it will continue with code after the last curly brace.
When to use this construct:When you are choosing between more than 2 actions.
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" );
switch( variable_integer_expression ) { case constant_integer_expression_1 : BB1; case constant_integer_expression_2 : BB2; break; // ( Optional - See below ) case constant_integer_expression_3 : BB3; default: // Optional BB4; } // End of switch on . . . |
![]() |
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 */ |
![]() |
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': EnterData(); break; case 'H': case 'h': case '?': PrintMenu(); break; case 'Q': case 'q': done = true; break; // could have more choices..... case 'C': case 'c': /* Code here to handle Calculations */ break; // end with the default for invalid input default: printf( "Error - %cinvalid\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;