ABSTRACT

This chapter teaches the computer science and IT students how to use the if and switch selection statements to control the flow of their program and make it select a particular execution path from a set of alternatives. It also introduces the conditional operator ?:, which can be used to form conditional expressions. The if statement controls program flow based on whether a condition evaluates to true or false. A common error is to add a semicolon (;) at the end of the if statement, as you usually do with the most statements. The semicolon is handled as a statement that does nothing, that is, a null statement, and the compiler terminates the if. An if statement may contain other if and else statements, which in turn may contain others, and so on. To match if and else statements, the rule is that an else is associated with the closest unmatched if inside the same group of statements.