Sie sind auf Seite 1von 12

Control Statements

Chapter 6
Introduction
• So far, the statements were executed in the same order
in which they appeared within the program--each
execution was executed once and only once.
• These are simple but do not include any logical control
structures.
• Various control structures are
– Branching: if-else statement
– Looping: while statement, do-while statement, for statement
– Nested control statement
– switch statement
– break statement
– continue statement

– Comma operator
– goto statement
Branching: if-else Statement
if Statement
• The if instruction carries out a logical test and takes one
of two possible actions depending on the outcome of the test.
Syntax: if (expr) statement
• statement will be executed only if expr has a non-zero
value (i.e. it is true); otherwise will be ignored, e.g.
if (first > second)
printf(“First number is bigger\n”);
• Compound statements (more than one statement) can also
be part of statement.
• expr is used as a logical test and can include logical
operators, e.g.
if ((balance < 1000)|| (status = ‘R’))
printf(“%f\n”, balance);
Branching: if-else Statement
if-else Statement
• Used when the program requires to take specific action if a
condition is true and another action if a condition is false,
e.g.
if (first == second)
printf(“Both numbers are equal\n”);
else
printf(“Numbers are not equal\n”);

Nested if-else Statement


• Embedding if-else statements, one within another, e.g.
if (expr1) if (expr2) statement1
else statement2
else if (expr3) statement3
else statement4
Looping
• Useful when we need a programming facility to repeat
certain work
Looping
for Statement
• Useful when number of passes through the loop is known
in advance
• Most commonly used looping statement:
Syntax: for (expr1; expr2; expr3) statement
• Looping action continues as long as the value of expr2 is
non-zero (i.e. true)
• All three expressions need not be included but ; are a must
• Equivalent to
expr1;
while (expr2){
statement;
expr3
}
Looping
for Statement
switch Statement

• Causes a particular group of statements to be chosen


from several available groups
• Selection is based upon the current value of an
expression included in the switch statement
Syntax: switch (expr) statement
• expr results in integer value, may also be of type
character as individual characters have individual integer values
• statement is a compound statement that specifies
alternate courses of action
• Each alternative is expressed as a group of one or more
individual statements within the overall embedded statement
switch Statement

• An alternate to the use of nested if-else statements


Nested Control Structures
• Nesting (embedding) of loops or branching is possible
• Inner and outer loops need not be generated by the same
type of control structure
• One loop must be completely embedded within the other,
i.e. there should be no overlapping

break Statement
• Used to terminate loops or exit from a switch
• Can be used within a for, while, do-while or switch
statements
• In a for, while or do-while, control will be transferred
out of the loop
• Nested for, while, do-while or switch causes a transfer of
control out of the immediate enclosing statement, but not
out of the outer surrounding statements
continue Statement
• Used to bypass the remainder of the current pass through
a loop
• Loop does not terminate when a continue statement is
encountered, rather the remaining loop statements are
skipped and the computation proceeds directly to the next
pass through the loop
• Can be included within a for, while, do-while or
switch statement
Comma Operator
• Used primarily in conjunction with the for statement
• Permits two different expressions to appear in situations
where only one expression would ordinarily be used; these
expressions will be evaluated from left to right
for (e1a, e1b; e2; e3) statements or
for (e1; e2; e3a, e3b) statements
goto Statement
• Used to alter the normal sequence of program execution by
transferring control to some other part (i.e. statement) of
the program
Syntax: goto label
• label is an identifier used to label the target statement to
which the control will be transferred
• Target statement must be labelled, e.g.
while (x <=1000)
{
if (x < 0)
goto errorcheck;

}
Target statement errorcheck: {
printf(“Error”);
}

Das könnte Ihnen auch gefallen