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

CO110 Computer Programming6

The document discusses decision making and branching statements in C programming. It defines decision making as changing the order of statement execution based on conditions. There are two types of branching statements: conditional statements like if and switch that execute different code blocks based on logical/relational expressions, and unconditional statements like goto that unconditionally change execution flow. Specific conditional statements like if, if-else, nested if-else, and else-if ladder are explained with examples.

Uploaded by

sauravk2424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

CO110 Computer Programming6

The document discusses decision making and branching statements in C programming. It defines decision making as changing the order of statement execution based on conditions. There are two types of branching statements: conditional statements like if and switch that execute different code blocks based on logical/relational expressions, and unconditional statements like goto that unconditionally change execution flow. Specific conditional statements like if, if-else, nested if-else, and else-if ladder are explained with examples.

Uploaded by

sauravk2424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 6

Decision Making and Branching


What is decision making and branching ?
 A ‘C’ program is a set of statements which are normally
executed sequentially in the order in which they
appear .
 However sometimes we need to change the order of
execution of the statements based on certain
conditions .
 The statements which help us to change the order of
execution are called as decision making and branching
statements.
Types of Branching statements
 The various branching statements available in C programming
language are broadly classified into :
 Conditional Branching statements
 Here the execution shifts to the desired statement
depending upon the evaluation of the conditional
expression.
 The conditional expression usually consists of logical and
relational operations which is evaluated to either zero
(false) or non-zero(true)

 Examples for Conditional branching statements are:


 if statement
 switch statement
Types of Branching statements cont ….
Unconditional Branching statements
 Unlike the conditional statements, here the statement
execution shifts unconditionally to another part of
the program.
 i.e. there is no evaluation of any conditional
expression to zero or non zero.
 There are also known as jump statements.
 Examples for Unconditional branching statements
are:
 goto statement
 break statement
 continue statement
 return statement
If Statement
 The if statement is a powerful decision making
statement. It is basically a two-way Decision making
statement.
 The if statement may be implemented in different
forms depending on the complexity of the problem .
 Simple if statement
 if…..else statement
 Nested if…..else statement
 else…if ladder
Simple if statement
 The general form of simple if statement is as following
 if(conditional-expression)
{
statement-block ;
}
statement-x ;
In the above example
 if the ‘conditional-expression’ results into true then the statement-
block’ will be executed.
 otherwise the ‘statement-block’ will be skipped and the control will
jump to the ‘statement –x’.

when the ‘conditional-expression’ results true then both the


‘statement-block’ and ‘statement–x’ will executed sequentially.
Flowchart for simple if statement
C program to demonstrate if statement
/* A program to understand the SIMPLE if STATEMENT */
#include<stdio.h>
main()
{
int num;

printf("Enter the value for num : ");


scanf("%d",&num);
There is no semi colon after
if(num%2==0) if statement
{
printf(“Number %d is an even number",num);
}
}
if …else statement
The general form of if….else statement is as following:
if(conditional-expression)
{
statement-block 1 ;
}
else
{
statement-block 2 ;
}
if …else statement cont…
 In if…else statement
 If the ‘conditional-expression’ results true then the
‘statement-block 1’ of if part will be executed
 If the ‘conditional-expression’ results false then the
‘statement-block-2’ of else part will be executed
skipping the ‘statement-block1’ of if part.
Flowchart for if else statement
C program to demonstrate if…else statement
/* A program to understand if else STATEMENT */
#include<stdio.h>
main()
{
int num;

printf("Enter the value for num : "); There is no conditional


scanf("%d",&num); expression required for else
statement
if(num%2==0)
{
printf(“Number %d is an even number",num);
}
else
{
printf(“Number %d is an odd number “,num );
}
}
Nested if … else statement
 The general form of Nested if….else statement is as following

if(conditional-expression1)
{
if (conditional-expression2)
{
statement-block-1 ;
}
else
{
statement-block-2 ;
}
}
else
{
statement-block-3 ;
}
statement-block-4;
Nested if … else statement cont…
 In case of above Nested if….else statement
 if ‘conditional-expression 1’ of outermost if statement
results false then control goes to statement-block3
with statement-block1 and 2 skipped out .
 If true the control goes to check ‘conditional-
expression2’ of inner if statement .
 If ‘conditional-expression2’ results true then ‘statement-
block1’ will be executed
 Else if false control will jump to ‘statement-block2’ will be
executed.
Flowchart for nested if else statement
C program to demonstrate nested if else statement
#include<stdio.h>
main()
{ Note the double
int num1,num2,num3; forward slashes used
printf("Enter three numbers: "); below
scanf("%d %d %d", &num1, &num2, &num3);
Comment lines to improve
if (num1>=num2) readibility of code
{
if(num1>=num3) //Nested if statement within if
printf("Largest number = %d",num1);
else
printf("Largest number = %d“,num3); //Nested else statement within if
}
else
{
if(num2>=num3) //Nested if statement within else
printf("Largest number = %d“,num2);
else //Nested else statement within else
printf("Largest number = %d“,num3);
}
}
C programs for nested if else statement
 Program to check if a given year is a leap year or not.
 Hint: a leap year is a year divisible by 4. Also if it is divisible by 400 but
not 100. E.g 1900 not a leap year but 2000 is a leap year.

 Program to check if a given character is a digit or an alphabet.


 If it is an alphabet check if it is vowel or not.
 If it is a digit check if it is odd or not.

 Program to check if a triangle is equilateral, isosceles or scalene


given the three sides. Also check if the triangle can be formed.
 Hint: A triangle cannot be formed if the greatest side is larger than
the sum of the other two sides.
Dangling else problem
 In Nested if .. else statements
 when a matching else is not available for an if.
 So in cases where it is not required to have a false condition, the
dangling else situation appears.
 The C compiler see’s that the else statement is always paired with
the most recent unpaired if.
 So in cases where we have two if statements and only one else
statement, the else will be paired with the second if statement.
 Solution is to use proper flower brackets { } to make the else join
with the proper if statement.
Consider the problem where we have to see if a number is divisible by both 2 and 3.
else associated with improper if else associated with proper if

int num; int num;


printf(“ enter the number”); printf(“ enter the number”);
scanf(“%d”,&num); scanf(“%d”,&num);

if( num%3==0 ) if( num%3==0 )


{
if ( num%2==0 ) if ( num%2==0 )
{ {
printf(“ divisible by both”); printf(“ divisible by both”);
} }
else }
{ else
printf(“ Not divisible by 3”); {
} printf(“ Not divisible by 3”);
}
else …if ladder statement
 The general form of else…..if ladder statement is as following: -
if(conditional-expression1)
{
statement-block-1 ;
}
else if(conditional-expression2) A conditional expression is
{ required for else if statement.
statement-block-2 ;
}
else if(conditional-expression3)
{
statement-block-3 ;
}
else there must be at least one
{ space between else and if
statement-block-4 ; keywords
}
else …if ladder statement cont …
 In case of else….If ladder
 The control checks the conditional-expressions from top
to bottom
 when a conditional expression results true then the
statement-block of that condition is executed and all the
remaining conditional statements will be skipped out .
 If all conditional-expression results false then the
‘statement-block4’ of else part is executed .
 This type of conditional statement is used where you
have to choose one option from multiple options .
Flowchart for else if ladder statement
C program to demonstrate else if ladder statement
#include<stdio.h>
main( )
{
int day;

printf(“ \n Enter day of week as numbers from 1 to 7");


scanf("%d",&day);
if(day==1)
printf("Day is Monday");
else if(day==2) There is no comma after
printf("Day is Tuesday"); the else if condition
else if(day==3)
printf("Day is Wednesday");
else if( day==4)
printf("Day is Thursday");
else if(day==5)
printf("Day is Friday");
else if(day==6) Note : else statement is not
printf("Day is Saturday"); mandatory in else if ladder
else program
printf("Day is Sunday");
}
C programs for else if ladder

Program to check in which quadrant a point lies given


it’s x and y axes coordinates.

Program to display BMI category given the height(in


meters) and weight( in kilograms) of a person.
 BMI = weight/(height*height)
Category BMI
Underweight < 18.5
Normal 18.5 - 24.9
Overweight 25 – 29.9
Obese > 30
switch statement
 The use of if statements becomes complex when the number of choices
increases and hence the code length.
 To overcome this issue, C has a switch statement which is a multi decision
making statement.

 The general form of switch statement is as following: -

switch(expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
case value-3: block-3;
break;
default: block-4;
}
switch statement cont…..
 In switch statement the value of a given variable or
expression is compared against a list of case values.
 When a match is found , the block of statement inside that
particular case is executed.
 When a match is not found, the default case statement block
is executed.
 It is not mandatory to have a default case.
 We use a break statement at the end of each case block to
prevent execution of subsequent cases.

Also in switch statement, only expressions which result


in an integer or a character value can be used. Otherwise
any integer or character constant can be used.
Flowchart for switch statement
C program to demonstrate switch statement
#include<stdio.h>
main( )
{
int day;

printf(“\n Enter day of week as numbers from 1 to 7");


scanf("%d", &day);
case and break are keywords
switch(day)
{
which are lowercase.
case 1: printf("Day is Monday");
break;
default case is not mandatory
case 2: printf("Day is Tuesday"); break;
There is a whitespace between
case 3: printf("Day is Wednesday"); break;
case 4: printf("Day is Thursday"); break;
case and the switch value.
case 5: printf("Day is Friday"); break; Also a : between switch value
case 6: printf("Day is Saturday"); break; and case statements
case 7: printf("Day is Sunday"); break;
}
C programs for switch

Program to implement a simple calculator on two


integer values.
Program to display the number of days in a month
given the month and year.
Program to display the grade of a student on the input
of marks in 5 subjects. ( Max= 100 Min =0)
Total Grade
>=450 A
375 - 449 B
300 - 374 C
<=299 D
goto statement
 C supports the goto statement to branch unconditionally from
one point to another point In the program .
 goto requires a label in order to identify the place where the
branch is to be made.
goto label;
 A label is any valid variable name and must be followed by a
colon.
label :
 goto statements can be used in two ways depending upon
the positon of the label with respect to it:–
 Forward Jump.
 Backward Jump.
Types of goto statements
 Forward Jump.
goto label;
…………..
label:
statement ;
 Backward Jump Note: We can implement
looping style when goto is
label:
written in the backward jump
statement; fashion.
…………...
goto label;
Use of goto statement is highly discouraged in high
level programming language because it makes difficult
to trace the control flow of a program, making the
program hard to understand and hard to modify.
C program to demonstrate goto
#include<stdio.h>
start label implements backward jump.
main()
check label implements forward jump.
{
int num;

start:
printf(“ enter the number”);
scanf(“%d”, &num); Now if we enter a number 7 for e.g
if (num <=0) 7 % 3 == 1
goto start;
It will skip the goto check label and
if( num%3==0) execute the printf(“ Not divisible by 3”);
goto check;

printf(“ Not divisible by 3”); It will then execute the check label which
goto end; is not required.
check: if ( num%2==0 ) To skip this we have a new label end
{
printf(“ divisible by both 2 and 3”);
}
end: printf(“ Skipped check label”);
}
 Which are the unconditional branching statements in
C? Explain with an example.
Explain the different forms of “if” statement.
Why is switch called a multi-decision making
statement?
Mention the differences between Nested if-else and
switch statement.

You might also like