DECISION MAKING & BRANCHING
❑ Source collected by : Rabeya Akter Bithi
Presented by:
❑ Script created by : Fatema Tuz Zohora &
Razia Sultana Anika Fatema Tuz Zohora & Md. Al – Imran
❑ Slides created by : Md. Al – Imran
Why is decision making & branching requisite in c program?
Types of decision making statement
Decision making
if
switch statement
statement
If statement
a) If decides whether a certain statement/block of statement will be executed or not.
b) Example: if a certain condition is true then a block of statement is executed
otherwise the statement won’t be executed.
Syntax- Flowchart-
Entry
if (condition)
{
//statement to execute if Test T
//condition is true expression
}
F
Different forms of the “ if-statement ”
If-else statement is used to carry
If statement is used to control the flow out a logical test and then take one
of execution of statements Simple of two possible actions depending
If else
if on the result of the test.
If
stateme
nt
Nestin
Else if
g if
ladder
else
Nesting if else statement are used Else if ladder statements are used,
when a series of decisions are concerned when multipath decisions are concerned. A
,we may have to employ more than one if multipath decision is a chain of ifs in which
else statement. the statements are associated with each
else is an if.
Simple if statement
Syntax- Example- Flowchart- Example-
#include<stdio.h> Entry Entry
if (test expression) int main()
{ int = 10;
If ( i > 15) Test True True
statement block; expressi If
} { on i >15
Statement x; printf(“10 is less then Statement 10 is less
15”); False block
False than 15
}
Printf(“I am not in if”); Statement-x I am not in if
}
next next
statement statement
If-else statement
Syntax- Example- Flowchart-
if (test condition-1) if (age > = 18) Entry
{ {
(true block statement); printf(“you are eligible for voting”);
} }
True False
else else Test
{ { condition
(false block statement); printf(“you are not eligible for voting”);
} } Statement
Statement
Statement x; Statement x; block block
Statement-x
Nesting if-else statement
Syntax- Example- Flowchart-
if (a > b)
Entry
{
if (a > c)
if (test condition-1) {
{ printf(“a is the largest number”);
} False Test True
if (test condition-2) condition
else
{ { 1
statement-1; printf(“c is the largest number”);
} } Statement-3
else } False Test True
{ else condition-
statement-2; { 2
if (c > b)
}
{
} printf(“c is the largest number”); Statement-2 Statement-1
else }
{ else
statement-3; {
Statement-x
} printf(“b is the largest number”);
Statement-x; }
}
Statement-x;
Else if ladder statement
Example- Flowchart-
Syntax- Entry
If (n > = 80) Test
if (condition-1) {
True
condition
False
1
statement-1; printf ("A+");
else if (condition-2) else if (n >=70)
Statement-1 True Test False
statement-2; printf ("A"); condition
2
else if (condition-3) else if (n > =60) Statement-2
statement-3; printf ("A-");
True Test False
else if (n >=50) condition
else if (condition-n) 3
printf ("B");
statement-n; Else if (n >=40) Statement-3
Test
else printf ("C");
True
condition
False
n
default statement else if
Statement-n Default
statement x ; printf ( "F") Statement
}
Statement
-x
Switch statement
▪ If is used in defining a particular statement from several statement in C language.
Example- Flowchart-
Syntax-
Entry
value =marks/10;
switch(value)
swtich(expression) {
{ case 10: Switch
case value-1: case 9: Expression
block-1; case 8:
break; printf("Well done Your grade is A+");break;
Code in case 1
case value-2: case 7: block
block-2; printf("Well done Your grade is A-");break;
case 6: Code in case 2
break; block
printf("Well done Your grade is B");break;
.................................. case 5:
................................... Code in case 3
printf("Well done Your grade is C"); break; block
default: case 4:
default block; printf("Well done Your grade is D");break; Code in default
block
break; default:
printf("Sorry Your grade is F!!");
}
Go to statement
▪ Go to statement is used to replace the control of one statement to another statement by up or
down conditionally and unconditionally .
Example- Flowchart-
Syntax-
int main()
goto Label; { Entry
.... statement1;
.... ... Label-1 Statement-1
if(condition)
Label:
goto label_name;
{
goto
statement n; statement2; Label-2 Statement-2
Label-3
} ...
label_name:
Label-3 Statement-3
statement3;
statement4;
...
return 0;
Thank you so much everyone.