zondag 17 juni 2007

The IF statement (ELSE) too

My previous experience with C++ I only was able to control decisions in loops, that was, the program decides wheter continue looping or not.

The if C++ if statement lets you use branching statements to decide among alternative actions.

What kind of a protection sheme should the program use, What menu choice has the user selected, Did the user enter a zero? C++ provides the IF and SWITCH statements to implement decisions, and they are the topic in this and probably other blogs.

When a C++ program must choose whether to take a particular action, you usually implement the choice with an IF statement. The if comes in two forms: if and if else.
Let's investigate the simple if first. It's modeled after ordinary English, as in "If you have a Captain Cookie card, you get a free cookie." The if statement directs a program to execute a statement or block if a test condition is true and to skip that statement or block if the condition is false. Thus an if statement lets a program decide whether a particular statement should be executed.

The syntax for the if statement is similar to that of the while syntax:

if (test-condition)
statement

A true test-condition causes the program to excecute statement, which can be a single statement or a block. A false test-condition causes the program to skip statement.
As with loop test conditions, an if test condition is type cast to a bool value, so zero becomes false and NONZERO becomes true.
The entire if construction counts as a single statement.

Most often, test-condition is a relational expression such as those used to control loops.

if (ch == ' ')
++spaces;

Checks to see if the relational expression is true, in this case the if statement compares if ch is equal to ' ', a space, then if so, it increments spaces.

The if else Statement
Whereas an if statement lets a program decide wheter a particular statement or block is executed, an if else statement lets a program decide which of two statements or blocks is executed. It's an invaluable statement for creating alternative courses of action.
The C++ if else statement is modeled after simple English, as in "If you have a Captain Cookie card, you get a Cookie Plus Plus, else you just get a Cookie d'Ordinaire." The if else statement has this general form:

if (test-condition)
statement1
else
statement2

If the test-condition is true, or NON ZERO, the program excecutes statement1 and skips over statement2.
Otherwise, when test condition is FALSE, or ZERO, the program skips statement1 and executes statement2 instead. So this code fragment:

if (answer == 1492)
cout << "That's right!\n";
else
cout << "You'd better review chapter 1 again.\n";

prints the first message if answer is 1492 and print the second message otherwise.
Each statement can be either a single statement or a block delimited by braces.
The entire if else construct counts syntactically as a single statement.

The if else if else Construction
Computer programs, like life, might present you with a choice of more than two selections. You can extend the C++ if else statement to meet such a need.
As you've seen, the else should be followed by a single statement, which can be a block. Because an if else statement itself is a single statement, it can follow an else:

if (ch == 'A')
a_grade++; // alternative # 1
else
if (ch == 'B') // alternative # 2
b_grade++; // subalternative #2a
else
soso++; // subalternative # 2b

If ch is not 'A', the program goes to else. There a second if else subdivides that alternative into two more choices. C++' s free formating enables you to arrange these elements into an easier-to-read format:

if (ch == 'A')
a_grade++; // alternative # 1
else if (ch == 'B')
b_grade++; // alternative # 2
else
soso++; // alternative # 3

This entire construction counts as one statement.

Geen opmerkingen: