Chapter 5 Decision Making and Branching - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Chapter 5 Decision Making and Branching

Description:

Programming in C Chapter 5 Decision Making and Branching main() { int i, num, sum; printf( Please input ten integers ); for( i=0; i – PowerPoint PPT presentation

Number of Views:4457
Avg rating:3.0/5.0
Slides: 44
Provided by: LIZH85
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5 Decision Making and Branching


1
Chapter 5 Decision Making and Branching
Programming in C
2
Programming in C
Chapt.5 Decision Making and Branching
Introduction
C program is a set of statements which are
normally executed sequentially in the order.
Many times it is required to alter the flow of
the sequence of instructions. C language provides
statements that can alter the flow of a sequence
of instructions. These statements are called
control statements. These statements help to jump
from one part of the program to another. The
control transfer may be conditional or
unconditional.
3
Programming in C
Chapt.5 Decision Making and Branching
Simple If Statement
The simplest form of the control statement is
the if statement.
The if statement takes the following form
if (expression ) statement next-statement
Flowchart of simple if control
4
Programming in C
Chapt.5 Decision Making and Branching
Example
The following program calculate the absolute
value of the integer inputted from keyboard.
include lt stdio.h gt void main ( ) int
number printf (Type a number) scanf
(d, number) if (number lt 0) number -
number printf (The absolute value is d \n,
number)



5
Programming in C
Chapt.5 Decision Making and Branching
Sometime more than one statements must be
executed if test expression is true, a pair of
braces must enclose them.
include lt stdio.h gt main ( ) int num1,
num2, num printf (Type two numbers)
scanf (dd, num1, num2) if (num1 lt
num2) num num1 num1 num2 num2
num printf (d, d\n, num1,num2)
include lt stdio.h gt main ( ) int num1,
num2, num printf (Type two numbers)
scanf (d d, num1, num2) if (num1 lt
num2) num num1, num1 num2, num2
num printf (d, d\n, num1,num2)
if(test expression) Statement1 Statement2
Statement3 ...
if (num1 lt num2) num num1, num1
num2, num2 num
6
Programming in C
Chapt.5 Decision Making and Branching
If the condition expression in if statement is a
compound expression takes the following form
if(condition1condition2) statement
The statement may be written as the following
form
if(condition1) If(condition2)
statement
7
Programming in C
Chapt.5 Decision Making and Branching
Example Input ten integers and calculate sum of
all numbers among range 0 to 100.
  • main()
  • int i, num, sum
  • printf( Please input ten integers )
  • for( i0 ilt10 i)
  • scanf( d , num )
  • if( num gt 0 num lt 100)
  • sumnum
  • printf( d , sum )

main() int i, num, sum printf(
Please input ten integers ) for( i0
ilt10 i) scanf( d , num )
if( num gt 0) if( num lt 100)
sumnum printf( d , sum )
8
Programming in C
Chapt.5 Decision Making and Branching
The if else statement
The if else statement is as following form
If (condition expression) statement_block1
else statement_block2
entry
test expression?
true
false
statement-block1
statement-block2
next statement
next statement
Flow chart of if else control
9
Programming in C
Chapt.5 Decision Making and Branching
ExampleProgram find whether the inputted letter
is uppercase or lowercase
void main ( )
char letter printf (Enter a letter) scanf
(c, letter)
if (letter lt z letter gt a) ) printf
(The letter is lowercase)
else printf (The letter is uppercase)

10
Programming in C
Chapt.5 Decision Making and Branching
Example2 Write a program to determines if a
year is a leap year.
include lt stdio.h gt
void main ( )
int year printf (Enter the year to be
tested) scanf (d, year)
if (year4 0year100! 0year4000)
printf (It is a leap year, \n)
else printf (No. It is not a leap year.
\n)
11
Programming in C
Chapt.5 Decision Making and Branching
Nested If Else Statement
The if statement may contain another if
statement that is known as nested if statement.
The if statement may be nested as deeply as
you need .
The nth if condition is nested in the (n-1)th
if (condition1) if (condition2) if
(condition3) statement-1 else
statement-2 else statement-3 else
statement-4
Statement-1 will be executed if all conditions
are true
12
Programming in C
Chapt.5 Decision Making and Branching
entry
test exp1?
true
false
test exp2?
true
false
statement4
statement3
test exp3?
true
false
statement1
statement2
next statement
Flow chart of nested ifelse statement
13
Programming in C
Chapt.5 Decision Making and Branching
Read following program and write what it will
output.
include lt stdio.h gt main ( ) int
a,b,c,big printf (Enter three numbers)
scanf (d d d, a, b, c) if (agtb)
if(agtc) big a else big c
else if (bgtc) big b else big
c printf (Largest of d,dd d,
a,b,c,big)
14
Programming in C
Chapt.5 Decision Making and Branching
The else If ladder
When a series of many conditions have to be
checked we may use the ladder else if statement
which takes the following general form.
if (condition1) 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
15
Programming in C
Chapt.5 Decision Making and Branching
The else If ladder
if (condition1) 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
16
Programming in C
Chapt.5 Decision Making and Branching
Example Write a program to find whether a
integer number inputted from keyboard is negative
or positive or zero
void main ( )
int num printf (Enter a integer number)
scanf (d, num)
if (num lt 0) printf (The number is negative)
else if (num gt 0) printf (The number is
positive)
else printf (The number is zero)

17
Example
  • Develop a program for the following problem.
  • Given a mark, determine its grade based on the
    table below
  • 74 lt mark lt 100 grade A
  • 64 lt mark lt 75 grade B
  • 54 lt mark lt 65 grade C
  • 39 lt mark lt 55 grade D
  • 0 lt mark lt 40 grade E
  • others error message

C Programming Language
17
18
Answer1
  • int mark
  • printf(Key-in the mark )
  • scanf(d,mark)
  • if ((mark gt 75) (mark lt 100))
  • printf("Grade A)
  • if ((mark gt 65) (mark lt 74))
  • printf(" Grade B)
  • if ((mark gt 55) (mark lt 64))
  • printf(" Grade C)
  • if ((mark gt 40) (mark lt 54))
  • printf(" Grade D)
  • if ((mark gt 0) (mark lt 39))
  • printf(" Grade E)
  • if ((mark gt 100) (mark lt 0))
  • printf(Input error\n)

19
  • int mark
  • printf(Key-in the mark )
  • scanf(d,mark)
  • if ((mark gt 100) (mark lt 0))
  • printf(Input error\n)
  • else if ((mark gt 75)
  • printf("Grade A)
  • else if (mark gt 65)
  • printf(" Grade B)
  • else if (mark gt 55)
  • printf(" Grade C)
  • else if (mark gt 40)
  • printf(" Grade D)
  • else
  • printf(" Grade E)

20
Programming in C
Chapt.5 Decision Making and Branching
The usage of multiple else If statement increases
the complexity of the program since when the
number of If else statements increase it affects
the readability of the program.
The switch statement removes these
disadvantages by using a simple and straight
forward approach.
21
Programming in C
Chapt.5 Decision Making and Branching
The Switch Statement
Switch statement allows a program to select
one statement (or statements block) from a set of
alternatives for execution.
During the execution of the switch statement only
one possible statement (or statements block) will
be executed and the remaining statements will be
skipped.
22
Programming in C
Chapt.5 Decision Making and Branching
statement-1 break
statement-2 break statement-n
break default next statement
entry
Selection process of the switch statement
switch expression
23
Programming in C
Chapt.5 Decision Making and Branching
switch (expression) case value-1
statement-1 break case value-2
statement-2 break case value-n
statement-n break default next
statement
statement-1 break
statement-2 break statement-n
break default next statement
entry
switch expression
24
Programming in C
Chapt.5 Decision Making and Branching
The general format of the Switch Statement is
switch (expression) case value-1
statement-1 break case value-2
statement-2 break case value-n
statement-n break default
switch (expression) case value-1
statement-1 case value-2 statement-2 cas
e value-n statement-n default
break
25
main() int a15,b21,m0 switch(a3) ca
se 0mbreak case 1m switch(b2)
defaultm case 0mbreak printf("d\n
",m)
26
Example switch program
case result num1 num2
break case / if (num2! 0) result
num1 / num2 else printf
(warning division by zero \ n)
result 0 break default printf (\n
unknown operator) result 0 break printf
(d, result)
include lt stdio.h gt main ( ) int num1,
num2, result char operator printf (Enter two
numbers) scanf (dy.d, num1, num2) printf
(Enter an operator) scanf (c, operator)
switch (operator) case result num1
num2 break case - result num1 num2
break
27
main() int i for(i0ilt3i)
switch(i) case 0 printf("d",i)break
case 1 printf("d",i)break case 2
printf("d",i)break default
printf("d",i)
0123
28
0000111223
main() int i for(i0ilt3i)
switch(i) case 0 printf("d",i) case
1 printf("d",i) case 2 printf("d",i)
default printf("d",i)
29
main() int i for(i0ilt3i)
switch(i) default printf("d",i) case
0 printf("d",i)break case 1
printf("d",i)break case 2
printf("d",i)break
01233
30
main() int i for(i0ilt3i)
switch(i) default
printf("d",i)break case 0
printf("d",i)break case 2
printf("d",i)break case 1
printf("d",i)break
0123
31
Programming in C
Chapt.5 Decision Making and Branching
5.8 Conditional Operator
The two symbols used to denote this operator
are the question mark (?) and the colon () .
The general format is, exp1 ? exp2 exp3
where exp1, exp2, exp3 are expression.
32
Programming in C
Chapt.5 Decision Making and Branching
5.8 Conditional Operator
exp1 ? exp2 exp3
The condition operator works as follows
exp1 is evaluated first.
If the exp1 is true then exp2 is evaluated and
its value becomes the value of the expression.
If exp1 is false, exp3 is evaluated and its value
becomes the value of the expression.
Note that only one of the expression is evaluated.
33
Programming in C
Chapt.5 Decision Making and Branching
An example will help you understand,
s ( x lt 0 ) ? -1 x x If x is less than
zero then s -1 If x is greater than or equal
to zero then s x x
s ( x lt 0 ) ? -1 x x equal to if ( x lt 0
) s -1 else s x x
34
Programming in C
Chapt.5 Decision Making and Branching
Example program illustrate how to find the
maximum value using conditional operator
include ltstdio.hgt main() int num1,num2,max
printf("please enter two numbers") scanf(d
d, num1, num2 ) max(num1gtnum2)?num1num2
printf(The maximum value is d, max)
35
Programming in C
Chapt.5 Decision Making and Branching
Example program illustrate how to find the
maximum value in three numbers.
include ltstdio.hgt main() int num1,num2,
num3,max printf("please enter three
numbers") scanf(ddd, num1, num2, num3
) max(num1gtnum2)?num1num2
max(maxgtnum3)? maxnum3 printf(The
maximum value is d, max)
36
Programming in C
Chapt.5 Decision Making and Branching
Example program illustrate how to find the
maximum value in three numbers.
include ltstdio.hgt main() int num1,num2,
num3,max printf("please enter three
numbers") scanf("ddd", num1, num2,
num3 ) max(num1gtnum2)? (num1gtnum3)?
num1num3 num2gtnum3? num2num3 printf("The
maximum value is d",max)
37
Programming in C
Chapt.5 Decision Making and Branching
Example program illustrate how to find the
maximum value in three numbers.
include ltstdio.hgt main() int num1,num2,
num3,max printf("please enter three
numbers") scanf(ddd, num1, num2, num3
) max(num1gtnum2)? ((num1gtnum3)? num1num3)
((num2gtnum3)? num2num3) max printf(The
maximum value is d, max)
38
Programming in C
Chapt.5 Decision Making and Branching
Read following program
include ltstdio.hgt main() int
num printf("please enter your number
now") scanf("d", number ) (numlt0)?printf("n
")(numgt0)?printf("p")printf("z")
Sample Program Output please enter your number
now 32 p
39
Programming in C
Chapt.5 Decision Making and Branching
The GOTO statement
The goto statement is simple statement used to
transfer the program control unconditionally from
one statement to another.
it might not be essential to use the goto
statement in a highly structured language like C.
The goto requires a label in order to identify
the place where the branch is to be made.
40
Programming in C
Chapt.5 Decision Making and Branching
Forward jump
goto label label statement
A label is a valid variable name followed by a
colon.
goto label label statement
goto label
Backward jump
label statement goto label
41
Programming in C
Chapt.5 Decision Making and Branching
The label is placed immediately before the
statement where the control is to be transformed.
The goto statement is discouraged in C, because
it alters the sequential flow of logic that is
the characteristic of C language.
42
Programming in C
Chapt.5 Decision Making and Branching
Example A program to calculate sum 123
n using goto statement
include lt stdio.h gt main ( ) int n, sum
0, i 0, printf (Enter a number) scanf
(d, n) loop i sum i if (i lt
n) goto loop printf (\n sum of d natural
numbers d, n, sum)
43
Programming in C
Chapt.5 Decision Making and Branching
END
Write a Comment
User Comments (0)
About PowerShow.com