Decision Making and Branching

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 43

Programming in C

Chapter 5
Decision Making and Branching
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.

03/13/17 Shandong Normal University 2


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 entry


the following form: True
test expression?
if (expression )
statement; statement
False
next-statement;
next-statement

Flowchart of simple if control

03/13/17 Shandong Normal University 3


Programming in C Chapt.5 Decision Making and Branching

Example:
The following program calculate the absolute value of the
integer inputted from keyboard.

# include < stdio.h >


void main ( )
{
int number;
printf (Type a number:);
scanf (%d, &number);
if (number < 0)
number = - number;
printf (The absolute value is %d \n, number);
}
03/13/17 Shandong Normal University 4
Programming in C Chapt.5 Decision Making and Branching

# Sometime
include < stdio.hmore> than one statements must be executed if
main
test( )expression is true, a pair of braces must enclose them.
{
int num1,
num1,num2,
intif(test num2,num;
num;
expression)
printf(Type
printf (Typetwotwonumbers:);
numbers:);
{ (%d
scanf
scanf (%d%d,
%d, &num1, &num2);
if (num1 <Statement1;
num2)
{ num = num1, num1 = num2, num2 =num;
Statement2;
printf num
(%d,=%d\n,
num1;num1,num2);if (num1 < num2)
Statement3;
} num1 = num2; num = num1, num1 = num2, num2
...
num2 =num; =num;
} }
printf (%d, %d\n, num1,num2);
}

03/13/17 Shandong Normal University 5


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(condition1&&condition2)
statement;

The statement may be written as the following form:

if(condition1)
If(condition2)
statement;

03/13/17 Shandong Normal University 6


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()
main()
{
{
int i, num, sum;
printf(int i,Please
num, sum;
input ten integers );
printf(
for( i=0; Please
i<10; i++) input ten integers );
{ for( i=0; i<10; i++)
{scanf( %d , &num );
if( num %d
scanf( > 0 ,
&&&num
num );
< 100)
sum+=num;
if( num > 0)
}
printf( if(
%dnum < 100)
, sum );
} sum+=num;
}
printf( %d , sum );
}
03/13/17 Shandong Normal University 7
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; entry
else
statement_block2; false true
test expression?

statement-block2 statement-block1

next
next statement
statement

Flow chart of if else control


03/13/17 Shandong Normal University 8
Programming in C Chapt.5 Decision Making and Branching

Example:Program find whether the inputted letter is uppercase


or lowercase
void main ( )
{
char letter;
printf (Enter a letter);
scanf (%c, &letter);
if (letter <= z && letter >= a) )
printf (The letter is lowercase);
else
printf (The letter is uppercase);
}
03/13/17 Shandong Normal University 9
Programming in C Chapt.5 Decision Making and Branching

Example2:
Write a program to determines if a year is a leap year.
# include < stdio.h >
void main ( )
{
int year;
printf (Enter the year to be tested);
scanf (%d, &year);

if (year%4 = = 0&&year%100! = 0||year%400==0)


printf (It is a leap year, \n);
else
printf (No. It is not a leap year. \n);
}
03/13/17 Shandong Normal University 10
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
The as you need
nth if condition is .
nested in the (n-1)th
if (condition1)
if (condition2)
if (condition3)
statement-
1;
else Statement-1 will
statement- be executed if
2; all conditions
else are true
statement-3;
03/13/17 Shandong Normal University 11
else
Programming in C Chapt.5 Decision Making and Branching
entry
false true
test exp1?

false true
test exp2?
statement4
statement3
false true
test exp3?

statement2 statement1

next statement

Flow chart of nested ifelse statement


03/13/17 Shandong Normal University 12
Programming in C Chapt.5 Decision Making and Branching

Read following program and write what it will output.


#include < stdio.h >
main ( )
{
int a,b,c,big;
printf (Enter three numbers);
scanf (%d %d %d, &a, &b, &c);
if (a>b)
if(a>c)
big = a ;
else
big = c ;
else if (b>c)
big = b ;
else
big = c ;
printf (Largest of %d,%d&%d = %d, a,b,c,big);
}
03/13/17 Shandong Normal University 13
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;
03/13/17 Shandong Normal University 14
statement-x;
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;
03/13/17 Shandong Normal University 15
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);

if (num
scanf < 0) &num);
(%d,
printf (The number is negative);
else if (num > 0)
printf (The number is positive);
else
printf (The number is zero);
} 03/13/17 Shandong Normal University 16
Example
Develop a program for the following problem.

Given a mark, determine its grade based on


the table below:

74 < mark < 100 grade = A


64 < mark < 75 grade = B
54 < mark < 65 grade = C
39 < mark < 55 grade = D
0 < mark < 40 grade = E
others error message
03/13/17 Shandong Normal University 17
C Programming Language 17
Answer1
int mark;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark >= 75) && (mark <= 100))
printf("Grade = A);
if ((mark >= 65) && (mark <= 74))
printf(" Grade = B);
if ((mark >= 55) && (mark <= 64))
printf(" Grade = C);
if ((mark >= 40) && (mark <= 54))
printf(" Grade = D);
if ((mark >= 0) && (mark <= 39))
printf(" Grade = E);
if ((mark > 100) || (mark < 0))
printf(Input error\n);
03/13/17 Shandong Normal University 18
int mark;
printf(Key-in the mark: );
scanf(%d,&mark);
if ((mark > 100) || (mark < 0))
printf(Input error\n);
else if ((mark >= 75)
printf("Grade = A);
else if (mark >= 65)
printf(" Grade = B);
else if (mark >= 55)
printf(" Grade = C);
else if (mark >= 40)
printf(" Grade = D);
else
printf(" Grade = E);
03/13/17 Shandong Normal University 19
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.

03/13/17 Shandong Normal University 20


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.

03/13/17 Shandong Normal University 21


Programming in C Chapt.5 Decision Making and Branching

entry Selection process of the switch statement

switch statement-
expression
1;
break;
expression = case value-
1
expression = case value-2 statement-2;
break;

expression = case value-n
statement-n;
break;
default:

next statement;
03/13/17 Shandong Normal University 22
Programming in C Chapt.5 Decision Making and Branching

entry switch (expression)


{
switch case value-1:
expression statement-1;
break;
expression = case value- case value-2:
1 statement-2;
expression = case value-2 break;

case value-n:
expression = case value-n statement-n;
break;
default:
}
next statement;
03/13/17 Shandong Normal University 23
Programming in C Chapt.5 Decision Making and Branching

The general format of the Switch Statement is :


switch (expression) switch (expression)
{ {
case value-1: case value-1:
statement-1; statement-1; break
case value-2: break;
statement-2; case value-2:
statement-2;
case value-n: break;
statement-n;
default: case value-n:
} statement-n;
break;
default:
03/13/17
}
Shandong Normal University 24
main()
{
int a=15,b=21,m=0;
switch(a%3)
{
case 0:m++;break;
case 1:m++;
}
switch(b%2)
{
default:m++;
case 0:m++;break;
}
printf("%d\n",m);
}

03/13/17 Shandong Normal University 25


Example switch program
case *: result = num1 * num2;
#include < stdio.h >
break;
main ( )
case /: if (num2! = 0)
{
result = num1 / num2;
int num1, num2, result;
else
char operator;
{
printf (Enter two numbers);
printf (warning : division b
scanf (%dy.d, &num1,
n);
&num2);
result = 0;
printf (Enter an operator);
}
switch (operator)
scanf (%c, &operator);
break;
{
default: printf (\n unknown operator);
case +:result = num1 +
num2; result = 0;
break; break;
case -: result = num1 printf (%d, result);
num2;
03/13/17 }
Shandong Normal University 26
break;
main()
{
int i;
for(i=0;i<=3;i++)
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
03/13/17 Shandong Normal University 27
main() 0000111223
{
int i;
for(i=0;i<=3;i++)
switch(i)
{
case 0: printf("%d",i);
case 1: printf("%d",i);
case 2: printf("%d",i);
default: printf("%d",i);
}
}

03/13/17 Shandong Normal University 28


main()
{
int i;
for(i=0;i<=3;i++)
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
03/13/17 Shandong Normal University 29
main()
{
int i;
for(i=0;i<=3;i++)
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
03/13/17 Shandong Normal University 30
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.

03/13/17 Shandong Normal University 31


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.


03/13/17 Shandong Normal University 32
Programming in C Chapt.5 Decision Making and Branching

An example will help you understand,

s = ( x < 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 < 0 ) ? -1 : x * x;
equal to
if ( x < 0 )
s = -1;
else
s = x * x;
03/13/17 Shandong Normal University 33
Programming in C Chapt.5 Decision Making and Branching

Example program illustrate how to find the maximum value


using conditional operator

#include <stdio.h>
main()
{
int num1,num2,max ;
printf("please enter two numbers:");
scanf(%d%d, &num1, &num2 );

max=(num1>num2)?num1:num2 ;
printf(The maximum value is %d, max);
}
03/13/17 Shandong Normal University 34
Programming in C Chapt.5 Decision Making and Branching

Example program illustrate how to find the maximum value in


three numbers.
#include <stdio.h>
main()
{
int num1,num2, num3,max ;
printf("please enter three numbers:");
scanf(%d%d%d, &num1, &num2, &num3 );

max=(num1>num2)?num1:num2 ;
max=(max>num3)? max:num3 ;
printf(The maximum value is %d, max);
}
03/13/17 Shandong Normal University 35
Programming in C Chapt.5 Decision Making and Branching

Example program illustrate how to find the maximum value in


three numbers.
#include <stdio.h>
main()
{
int num1,num2, num3,max ;
printf("please enter three numbers:");
scanf("%d%d%d", &num1, &num2, &num3 );
max=(num1>num2)? (num1>num3)? num1:num3: num2>num3?
num2:num3 ;
printf("The maximum value is %d",max);
}

03/13/17 Shandong Normal University 36


Programming in C Chapt.5 Decision Making and Branching

Example program illustrate how to find the maximum value in


three numbers.
#include <stdio.h>
main()
{
int num1,num2, num3,max ;
printf("please enter three numbers:");
scanf(%d%d%d, &num1, &num2, &num3 );

max=(num1>num2)? ((num1>num3)? num1:num3):


((num2>num3)? num2:num3) ;
max;
printf(The maximum value is %d, max);
} 03/13/17 Shandong Normal University 37
Programming in C Chapt.5 Decision Making and Branching

Read following program


#include <stdio.h>
main()
{
int num;
printf("please enter your number now:");
scanf("%d", &number );
(num<0)?printf("n"):(num>0)?printf("p"):printf("z");
}

Sample Program Output


please enter your number now: 32
p
03/13/17 Shandong Normal University 38
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.

03/13/17 Shandong Normal University 39


Programming in C Chapt.5 Decision Making and Branching

Forward jump
goto label;
A label is a valid
goto label;
variable name

followed by a
label:
colon.
statement;
label:
Backward jump statement;
label:
statement;

goto label;

goto label;
03/13/17 Shandong Normal University 40
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.

03/13/17 Shandong Normal University 41


Programming in C Chapt.5 Decision Making and Branching

Example:
A program to calculate sum = 1+2+3+ + n using goto
statement
#include < stdio.h >
main ( )
{
int n, sum = 0, i = 0,
printf (Enter a number);
scanf (%d, &n);
loop: i ++;
sum + = i;
if (i < n)
goto loop;
printf (\n sum of %d natural numbers = %d, n, sum);
}
03/13/17 Shandong Normal University 42
Programming in C Chapt.5 Decision Making and Branching

END

03/13/17 Shandong Normal University 43

You might also like