You can control the order in which statements are executed in
your C program using control statements like if
, if-else
, and for
loops. Control statements make decisions about what is to be executed
next in the program sequence.
An if
statement is a type of conditional
control statement. The format of an if
statement
is:
if (logical-expression) {statements}
where logical-expression
is the condition
to be tested, and statements
are the lines
of code that are to be executed if the condition is met.
if-else
statements are another type
of conditional control statement. The format of an if-else
statement is:
if (logical-expression) {statements} else {statements}
where logical-expression
is the condition
to be tested, and the first set of statements
are the lines of code that are to be executed if the condition is
met. If the condition is not met, then the statements following else
are executed.
for
loops are control statements that
are a basic looping construct in C. They are analogous to do
loops in FORTRAN. The format of a for
loop is
for (begin ; end ; increment) {statements}
where begin
is the expression that
is executed at the beginning of the loop; end
is the logical expression that tests for loop termination; and increment
is the expression that is executed at the
end of the loop iteration (usually incrementing a counter).