The if, if-else, switch, break, continue, and goto statements fall into the conditional branching category.
The general form of the if statement is
if (expression) {
statement1;
statement2;
.
.
.
}
Here expression is the conditional criterion. If expression is logical TRUE (that is, nonzero), the statements inside the
braces ({ and }), such as statement1 and statement2, are executed. If expression is logical FALSE (0), the statements
are skipped.
As an expansion of the if statement, the if-else statement has the following form:
if (expression) {
statement1;
statement2;
.
.
.
}
else {
statement_A;
statement_B;
.
.
.
}
Here if expression is logical TRUE, the statements controlled by if, including statement1 and statement2, are
executed. Otherwise, the statements, such as statement_A and statement_B, inside the statement block following the
else keyword are executed, if expression is logical FALSE.
The general form of the switch statement is
switch (expression) {
case expression1:
statement1;
case expression2:
statement2;
.
.
.
default:
statement-default;
}
Here the conditional expression, expression, is evaluated first. If the return value of expression is equal to the constant
expression expression1, then execution begins at the statement statement1. If the value of expression is the same as
the value of expression2, execution then begins at statement2. If, however, the value of expression is not equal to any
values of the constant expressions labeled by the case keyword, the statement, statement-default, following the
default keyword is executed.
You can add a break statement at the end of the statement list following each case label if you want to exit the switch
construct after the statements within a selected case have been executed.
Also, the break statement can be used to break an infinite loop.
There are times when you want to stay in a loop but skip over some of the statements within the loop. To do this, you
can use the continue statement provided by C.
The following gives the general form of the goto statement:
label-name:
statement1;
statement2;
.
.
.
goto label-name;
Here label-name is a label name that tells the goto statement where to jump. You have to place label-name in two
places: at the place where the goto statement is going to jump and at the place following the goto keyword. Also, the
place for the goto statement to jump to can appear either before or after the statement. Note that a colon (:) must
follow the label name at the place where the goto statement will jump to.
Friday, October 16, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment