3. Selection Statements - if/else, switch
if, with optional else: if( condition) some_statement; else some_other_statement;
statement can be one statement or { multiple statements in braces }
if( a < b) if( a < b) if( a < b) if( a < b) ++count; { ++count1; { ++count; else ++count1; c = a; --count2; c = a; } } else { --count2; c = b; } if( a < b) { ++count1; c = a; } else { --count2; c = b; } // conditional operator ?: // c = (a < b) ? a : b; same as: if( a < b) c = a; else c = b;if/else can be used in nested form; for an integer decision variable, use switch statement:
if( code == 10) switch( code) statement1; { else if( code == 11) case 10: statement1; break; statement2; case 11: statement2; break; else if( code == 13) case 13: statement3; break; statement3; default: statement4; break; else } statement4;Practice!