< Previous | Contents | Next >

Using the else Clause

You can add an else clause to an if statement to provide code that will only be executed if the tested expression is false. Heres the form of an if statement that includes an else clause:

if (expression)

statement1; else

statement2;

If expression is true, statement1 is executed. Then the program skips statement2 and executes the statement following the if suite. If expression is false, statement1 is skipped and statement2 is executed. After statement2 completes, the program executes the statement following the if suite.

46 Chapter 2 n Truth, Branching, and the Game Loop: Guess My Number


 

Introducing the Score Rater 2.0 ProgramCreating Two Ways to Branch