Chapter 6 Control Statements
Chapter 6 Control Statements
Chapter 6
1
Introduction
• A realistic C program may require that a logical test be carried
out at some particular point within the program.
• One of several possible actions will then be carried out,
depending on the outcome of the logical test. This is known
as branching. There is also a special kind of branching, called
selection, in which one group of statements is selected from
several available groups.
• In addition, the program may require that a group of
instructions be executed repeatedly, until some logical
condition has been satisfied. This is known as looping.
2
PRELIMINARIES
• First, we will need to form logical expressions that are either true or
false. To do so, we can use the four relational operators, <, <=, >, >=,
and the two equality operators, == and !=
• In addition to the relational and equality operators, C contains two
logical connectives (also called logical operators), && (AND) and II
(OR), and the unary negation operator !
3
Contd…
4
BRANCHING: THE if - else STATEMENT
• The if - else statement is used to carry out a
logical test and then take one of two possible
actions, depending on the outcome of the test
(i.e., whether the outcome is true or false).
• The simplest general form, the statement can
be written as if ( expression) statement
Where else might be optional.
5
Contd…
6
Contd…
• The general form of an if statement which
includes the else clause is
• if (expression) statement1 else statement2
7
Example of if-else
8
Nested if-else
• It is possible to nest (i.e., embed) if - else
statements, one within another.
9
Example
10
LOOPING: THE while STATEMENT
• The while statement is used to carry out
looping operations, in which a group of
statements is executed repeatedly, until some
condition has been satisfied. The general form
of the while statement is
• while ( expression) statement
11
Contd…
12
Contd…
• In some looping situations, the number of
passes through the loop is known in advance.
• Sometimes, however, the number of passes
through the loop is not known in advance.
• Rather, the looping action continues
indefinitely, until the specified logical
condition has been satisfied.
• The while statement is particularly well suited
for this second type of loop.
13
Example
• Lowercase to Uppercase Text Conversion: In this example
we will read a line of lowercase text character-by-character
and store the characters in a char-type array called letter .
• The program will continue reading input characters until an
end-of-line (EOF) character has been read.
• The characters will then be converted to uppercase, using
the library function toupper, and displayed.
14
Implementation of while loop
15
Contd…
16
Example
17
MORE LOOPING: THE do - w h i l e STATEMENT
18
Example
19
STILL MORE LOOPING: THE for STATEMENT
20
Example
• Averaging a List of Numbers:
• Calculates the average of a list of n numbers,
so that the looping action is accomplished by
means of a for statement.
21
Contd…
22
NESTED CONTROL STRUCTURES
23
Example
24
Input and Output
25
Converting Several Lines of Text to Uppercase
26
Input and Output
27
Encoding a String of Characters
• Let us write a simple C program that will read in a
sequence of ASCII characters and write out a
sequence of encoded characters in its place.
• If a character is a letter or a digit, we will replace it
with the next character in the character set, except
that Z will be replaced by A, z by a, and 9 by 0.
• Thus 1 becomes 2, C becomes D, p becomes q, and
so on. Any character other than a letter or a digit will
be replaced by a period (.) .
28
Example
29
Input and Output
30
More Examples
EXAMPLE 6.21
Repeated Compound Interest Calculations with Error
Trapping
EXAMPLE 6.22
Solution of an Algebraic Equation
31
THE switch STATEMENT
• The switch statement causes a particular group of
statements to be chosen from several available
groups.
• The selection is based upon the current value of an
expression which is included within the switch
statement.
• The general form of the switch statement is
• switch (expression) statement
where expression results in an integer value. Note that
expression may also be of type char, since individual
characters have equivalent integer values.
32
Contd…
33
Example
34
Example
35
Example
36
Example
Practice the EXAMPLE 6.26 Calculating
Depreciation.
37
THE break STATEMENT
• The break statement is used to terminate
loops or to exit from a switch.
• It can be used within a for, while, do -while, or
switch statement.
• The break statement is written simply as
break; without any embedded expressions or
statements.
38
39
THE continue STATEMENT
40
Example
41
Example
42
Input and Output
43
THE COMMA OPERATOR
45
Contd…
The most common applications were:
1. Branching around statements or groups of
statements under certain conditions.
2. Jumping to the end of a loop under certain
conditions, thus bypassing the remainder of the loop
during the current pass.
3. Jumping completely out of a loop under certain
conditions, thus terminating the execution of a loop.
46
Contd…
• The use of the structured features such as if-else, continue,
break are preferrable to the use of the goto statement
• Because the use of goto tends to encourage (or at least, not
discourage) logic that skips all over the program whereas the
structured features in C require that the entire program be
written in an orderly, sequential manner.
• For this reason, use of the goto statement should generally be
avoided.
47
Example
48
Thank You!!!
49