< Previous | Contents | Next >

Using the if Statement

Okay, its time to put the concepts of true and false to work. You can use an if statement to test an expression for truth and execute some code based on it. Heres a simple form of the if statement:

if (expression)

statement;

If expression is true, then statement is executed. Otherwise, statement is skipped and the program branches to the statement after the if suite.


Hi n t

image

Whenever you see a generic statement like in the preceding code example, you can replace it with a single statement or a block of statements because a block is treated as a single unit.

image

Using the if Statement 41


 

Introducing the Score Rater ProgramTesting true and falseInterpreting a Value as true or falseUsing Relational OperatorsNesting if Statements