Chapter-5
Decision Making and
Branching
Copyright © 2016 McGraw Hill Education, All Rights Reserved.
PROPRIETARY MATERIAL © 2016 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and
educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without
permission.
Introduction
• C program is a set of statements which are normally executed sequentially in
the order in which they appear.
• But, in practice, a number situations exist where the order of execution of
statements needed to be changed based on certain conditions or a group of
statements need to repeat until certain conditions are met.
• Decision-making statements (or control statements) in C are:-
• if statement
• switch statement
• Conditional operator statement
• goto statement
Decision making with IF Statement
• if statement is a powerful decision-making statement and is used to control
the flow of execution of statements.
• It takes the form- if (test expression)
• It allows the computer to evaluate the expression first and then, depending
on whether the value if the expression (relation or condition) is ‘true’ (non-
zero) or ‘false’ (zero), it transfer the control to a particular statement.
• e.g.-
if (bank balance is zero)
borrow money
if (room is dark)
put on lights
Fig. 5.1 Two-way branching
• Depending on the complexity of conditions to be tested, if statement may be
implemented as follows-
• Simple if statement
• if…else statement
• Nested if…else statement
• else if ladder
Simple IF Statement
• General form is-
if (test expression)
{
statement-block;
}
statement-x
• e.g.
……
if (category == sports){
marks = marks + bonus_marks;
} Fig. 5.2 Flowchart of simple if Control
printf(“%f”, marks);
……
Fig. 5.3 Illustration of simple if statement
Fig. 5.4 Use of if for counting
The if…else statement
• The if…else statement is an extension of the simple if statement. General
form is:
if (test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
Fig. 5.5 Flowchart of if......else control
Fig. 5.6 Illustration of if...else statement
Nesting of if…else Statements
General format: e.g:
if (sex is female)
if (test-condition-1)
{
{
if (balance > 5000)
if (test-condition-2)
{
{
bonus = 0.05 * balance;
statement-1;
}
}
else
else
{
{
bonus = 0.02 * balance;
statement-2;
}
}
}
}
else
else
{
{
statement-3;
bonus = 0.02 * balance;
}
}
statement-x;
balance = balance + bonus;
Fig. 5.7 Flow chart of nested if…else statements
Fig 5.8 Selecting the largest of three numbers
The else if ladder
• Another way of putting ifs together is available when multipath decisions are
involved. A multipath decision is a chain of ifs in which the statement
associated with each else is an if. The general form is:-
if (condition-1)
statement-1;
else if (condition-2)
statement-2;
else if (condition-3)
statement-3;
else if (condition-n)
statement-n;
else
default-statement;
statement-x;
Fig. 5.9 Flow chart of else..if ladder
• Consider an example of grading the students in an academic institution. The
grading is done according to the following rules:
Average marks Grade
80 to 100 Honours
60 to 79 First Division
50 to 59 Second Division
40 to 49 Third Division
0 to 39 Fail
if (marks > 79)
grade = “Honours”;
else if (marks > 59)
grade = “First Division”;
else if (marks > 49)
grade = “Second Division”;
else if (marks > 39)
grade = “Third Division”;
else
grade = “Fail”;
printf(“%s \n”, grade);
Fig. 5.10 Illustration of else..if ladder
The Switch Statement
• switch statement test the value of a given variable (or expression) against a
list of case values and when a match is found, a block of statements
associated with that case is executed.
• General form of switch statement is-
switch (expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
……
……
default:
default-block
break;
}
statement-x;
• The expression is an integer expression or characters.
• Value-1, value-2… are constants or constant expressions (evaluate to an
integral constant) and are known as case labels.
• block-1, block-2 are statement list and may contain zero or more statement.
• case labels end with a colon (:).
• When the switch is executed, the value of the expression is successfully
compared against values value-1, value-2,… If a case is found, then block of
statements following the case are executed.
• break statement signals the end of a particular case and causes an exit from
the switch statement.
• default is an optional case. When present, it will be executed if the value of
the expression does not match with any of the case values.
• ANSI C permits the use of as many as 257 case labels.
Fig. 5.11 Selection process of the switch statement
---
index = marks/10
switch (index)
{
case 10:
case 9:
case 8:
grade = “Honours”;
break;
case 7:
case 6:
grade = “First Division”;
break;
case 5:
grade = “Second Division”;
break;
default:
grade = “Fail”;
break;
}
printf(“%s \n”, grade);
----
The goto Statement
• goto statement is used in C to branch unconditionally from one point to
another in the program.
• goto requires a label in order to identify the place where the branch is to be
made.
• A label is any valid variable name, and must be followed by a colon. It can be
anywhere in the program.
• General form of goto and label statements are:
• If the label: is before the statement goto label; it is known as backward jump.
If the label: is after goto label; it is known as forward jump.
main(){
double x, y;
read:
scanf (“%f”, &x);
if (x<0) goto read;
y = sqrt (x);
printf (“%f %f \n”, x, y);
goto read;
}
enum
• Enumerated data type (enum) is a user-defined data type provided by ANSI
C. It is defined as:-
enum identifier {value1, value2,… valuen};
• The identifier is a user-defined enumerated data type which can be used to
declare variables that can have one of the values enclosed within the braces
(known as enumeration constants)
• After declaration, we can declare variable as-
enum identifier v1, v2,…vn;
continue
• Continue statement is mostly used inside loops. Whenever it is encountered
inside a loop, control directly jumps to the beginning of the loop for next
iteration, skipping the execution of statements inside loop’s body for the
current iteration.
conio.h
• conio.h is a C header file used mostly by MS-DOS compilers to provide
console input/output. It is not part of the C standard library or ISO C, nor is it
defined by POSIX.
• This header declares several useful library functions for performing "console
input and output" from a program. Most C compilers that
target DOS, Windows 3.x, Phar Lap, DOSX, OS/2, or Win32 have this header
and supply the associated library functions in the default C library. Most C
compilers that target UNIX and Linux do not have this header and do not
supply the library functions.
• getch() function prompts the user to press a character and that character is
not printed on screen, getch header file is conio.h.
• getche() is used to get a character from the console and echoes to the
screen.
• gets() accepts single or multiple characters of string including spaces from
the standard Input device.
• putch() displays any alphanumeric characters to the standard output device.
It displays only one character at a time.
• puts() displays single or multiple characters of string including spaces to the
standard Output device.
Fig. 5.12 Illustration of the conditional operator
Fig. 5.13 Use of the goto statement
Fig. 5.14 Calculation of range of values
(Contd.)
Fig. 5.15 Pay-bill calculations
(Contd.)
Fig. 5.15 Pay-bill calculations
Fig. 5.15 Pay-bill calculations