|
You are in Section 2 of 9, Article 2.2 of 2.6, Part 2.2.1 of 2.2.3
There are generally three versions of selection statements to cover:
- one-way
- two-way
- multi-way
One-way decision statements do some particular thing or they do nothing. Two-way decision statements do one thing or do another. Multi-way decision statements can do many different things depending on the result of an expression.
One-Way Decisions
One-way decisions are handled with an "if statement" and either do some particular thing or do nothing at all. The decision is based on a logical expression which either evaluates to true or false, a value of 1 or a value of 0, respectively.
It is important to remember that logical expressions always evaluate to a value of true or false; a nonzero value is always true, and a value of zero is always false.
If the logical expression is evaluated to true, then the corresponding statement is executed; if the logical expression evaluates to false, control goes to the next executable statement.
The form of a one-way decision statement is as follows:
The stmtT can be a simple or compound statement. Simple involves a single statement. Compound involves one or more statements enclosed with curly braces { }. A compound statement is called a block statement.
Example 1: simple statement
Example 2: compound statement
For an example of how one-way decision statements can be used in a program, consider the following complete program:
Another type of selection statement is the two-way decision statement. Read on for more...
Next: Two-Way Decisions
You are in Section 2 of 9, Article 2.2 of 2.6, Part 2.2.1 of 2.2.3
[Back to Top]
|