0% found this document useful (0 votes)
3 views

Chapter 6 Control Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 6 Control Statements

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

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…

• The conditional operator ? : also makes use of an expression


that is either true or false . An appropriate value is selected,
depending on the outcome of this logical expression.
• status = (balance == 0) ? 'C' : '0‘
• There are three different kinds of statements in C: expression
statements, compound statements and control statements

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

• When a loop is constructed using the while statement


described , the test for continuation of the loop is carried out
at the beginning of each pass.
• Sometimes, however, it is desirable to have a loop with the
test for continuation at the end of each pass. This can be
accomplished by means of the do - while statement.
• The general form of the do - while statement is
• do statement while (expression);

18
Example

19
STILL MORE LOOPING: THE for STATEMENT

• This statement includes an expression that specifies


an initial value for an index, another expression that
determines whether or not the loop is continued,
and a third expression that allows the index to be
modified at the end of each pass.
• The general form of the for statement is
• for ( expression 1; expression 2; expression 3) 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

• Loops, like if - else statements, can be nested, one


within another.
• Repeated Averaging of a List of Numbers:
• Suppose we want to calculate the average of several
consecutive lists of numbers.
• If we know in advance how many lists are to be
averaged, then we can use a for statement to control
the number of times that the inner (averaging) loop
is executed.

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

• The continue statement is used to bypass the remainder of the current


pass through a loop.
• The 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.
• The continue statement can be included within a while, a do - while or a
for statement.
• It is written simply as
continue;
without any embedded statements or expressions.

40
Example

41
Example

42
Input and Output

43
THE COMMA OPERATOR

• We now introduce the comma operator (,) which is used


primarily in conjunction with the for statement.
• This operator permits two different expressions to appear
in situations where only one expression would ordinarily be
used. For example, it is possible to write
• for( expression 1a, expression 1b; expression 2; expression
3) statement
• for ( expression 1; expression 2; expression 3a, expression
3b) statement
• Practice the example-EXAMPLE 6.32 Searching for
Palindromes 44
THE goto STATEMENT
• The goto statement is used to alter the normal sequence of
program execution by transferring control to some other part of
the program.
• In its general form, the goto statement is written as
goto label;
where label is an identifier that is used to label the target
statement to which control will be transferred.
• Control may be transferred to any other statement within the
program.
• To be more precise, control may be transferred anywhere
within the current function.

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

You might also like