Hardware Reference
In-Depth Information
5.4.1 If Statement
The if statement is a conditional statement. The statement associated with the if state-
ment is executed on the basis of the outcome of a condition. If the condition evaluates to non-
zero, the statement is executed. Otherwise, it is skipped. The syntax of the if statement is
if (expression)
statement;
Here is an example of an if statement.
if (a . b)
sum 15 2;
The value of sum will be incremented by 2 if the variable a is greater than the variable b .
5.4.2 If-Else Statement
The if-else statement handles conditions where a program requires one statement to be
executed if a condition is nonzero and a different statement if the condition is zero. The syntax
of an if-else statement is
if (expression)
statement 1
else
statement 2
The expression is evaluated. If it is true (nonzero), statement 1 is executed. If it is false (zero),
statement 2 is executed. Here is an example of the if-else statement.
if (a ! 5 0)
r 5 b;
else
r 5 c;
The if-else statement can be replaced by the ?: operator. The statement
r 5 (a ! 5 0)? b : c;
is equivalent to the previous if-else statement.
5.4.3 Multiway Conditional Statement
A multiway decision can be expressed as a cascaded series of if-else statements. Such a
series looks like this.
if (expression 1 )
statement 1
else if (expression 2 )
statement 2
else if (expression 3 )
statement 3
. . .
else
statement n
Here is an example of a three-way decision.
if (abc . 0) return 5;
else if (abc 55 0) return 0;
else return 2 5;
 
Search WWH ::




Custom Search