SlideShare a Scribd company logo
1 of 42
1
Let Us
Introduction
 “Decision making and branching” is one of the most
important concepts of computer programming.
 Programs should be able to make logical (true/false)
decisions based on the condition provided.
 Every program has one or few problems to solve. In order to
solve those particular problems important decisions have to
be made depending on the nature of the problems.
 Generally C program execute it’s statements sequentially.
But in order to solve problems we may have some situations
where we have to change the order of executing the
statements based on whether some conditions have met or
not. So controlling the execution of statements based on
certain condition or decision is called decision making and
branching.
START
Read User
Input
Condition
?
Perform Some
Tasks
Perform Some
Other Tasks
Show Result
END
Making Decision
Condition not Satisfied
[False]
Condition Satisfied
[True]
Branch 2Branch 1
Branching
Holiday Trip Problem
 Consider the fact that you and some of your friends
have planed to go out for a holiday trip after the Spring
Semester, 2014.
 You have also decided that if you have got received
money 10000 taka or more from your parent then your
will go out for a foreign trip. Otherwise, if the allotted
money is less than 10000 then you will go out for a
country side trip.
 Now you are supposed to design a program to solve
this problem.
START
Money Received
15000
Received
Money>=10000
?
Holiday
Enjoyed
END
Condition not Satisfied
[False] Condition Satisfied
[True]
Holiday Trip Problem
Decision Making & Branching in C
 C language possesses decision making and branching
capabilities by supporting the following statements:
1. If statement
2. Switch statement
3. Conditional operator statement
4. goto statement
 These statements are knows as decision making
statements. They are also called control statements as the
control the flow of execution.
IF Statement
 The if statement is a powerful statement for decision making
and is used to control the flow of execution of statements. It
is basically a two-way decision making statement and is
used in conjunction with an expression. It takes the
following structure:
if (test-condition)
 It allows the computer to evaluate the expression first and
then depending on whether the value of the expression or
condition is true or false, it transfer the control to a
particular statement. This point of program has two paths to
follow, one for the true condition and the other for the false
condition.
IF Statement
Test
Condition ?
Entry
TrueFalse
Example:
If(Pocket balance is zero)
Borrow money;
IF Statement
 The if statement can be implemented if four different forms
depending on the complexity of the conditions to be tested.
The four forms are:
1. Simple if statement
2. If else statement
3. Nested if else statement
4. Else if ladder
Simple if Statement- Structure
 The general form of a simple if statement is:
if (test_condition)
{
statement-block;
}
statement x;
Simple if Statement
 The statement block may consists of a single statement or a
group of statements.
 If the test_condition is satisfied then the statement block
will be executed first then the statement-x will be executed
after completing the execution of statement block.
 Otherwise if the test_condition doesn’t satisfied then, the
statement block will be skipped and the execution will jump
to the statement-x.
 So in short when the condition is true then both the
statement block and the statement-x are executed but in
sequence.
Simple if Statement- Flowchart
Entry
Test_Condition
?
Statement-X;
Statement-block;
Next Statement;
[ True ]
[ False ]
Simple if Statement
 Important things to notice that if you want to control a
single statement using the if condition then no need to
create a block using the curly bracy.
 The curly bracy is used to create statement block when it is
necessary to include multiple statements under the same if
condition.
Simple if Statement-Example
 Example with block of statement:
if (marks>=90)
{
marks=marks+ bonus_marks;
grade=“A+”;
}
printf(“The mark achieved:marks” , %d);
Condition controlled
statement
Simple if Statement
 Example of single statement:
if (marks>=90)
marks=marks+ bonus_marks;
printf(“The mark achieved:marks” , %d);
Condition controlled
statement
Simple if Statement
 What happens if multiple statements follows
after the if condition without using the curly
brace?
 Answer: Then the statement immediately following the
if condition is considered as the controlling statement
of the if condition. if the condition is true the controlled
statement is executed otherwise not. Rest of the
statements are executed sequentially.
Simple if Statement
 Example:
if (marks>=90)
marks=marks+ bonus_marks;
grade=“A+”;
printf(“The mark achieved:marks” , %d);
Condition controlled
statement
Animation of How Simple if Works
if else Statement
 If the test condition is true then the true block statements,
immediately following the if statements are executed;
 Otherwise the false block statements are executed.
 In short either true-block or false-block of statements will
be executed, not both.
 But in both cases the control is transferred subsequently to
the statement-x as it is an independent (not controlled by
the if else statement) statement.
 It is also called two way conditional branching
if else Statement- Structure
 The if else statement is an extension of the simple if
statement. The general form is :
if (test_condition)
{
True block statements;
}
else
{
False block statements;
}
statement-x;
if else Statement- Flowchart
Entry
Test_Condition
?
Statement-X;
True Statement-block;
Next Statement;
[ True ][ False ]
False Statement-block;
if else Statement- Example
 Example with block of statement:
if (marks>=40)
{
marks=marks+ bonus_marks;
grade=“passed”;
}
else
{
marks=marks;
grade=“failed”;
}
printf(“The mark achieved:marks” , %d);
True block statement
False block statement
Animation of How if else Works
Nested if else Statement
 Using “if…else statement” within another “if…else
statement” is called ‘nested if statement’. “Nested if
statements” is mainly used to test multiple conditions.
 It is called nested conditional branching.
 They can be structured using following syntax:
Nested if else Statement- Structure
if (test_condition)
{
if (test_condition)
{
statement-block;
}
else
{
statement-block;
}
}
else
{
statement-block;
}
statement-x;
Nested if else
Nested if else -Exampleif (gender==female)
{
if (age<10)
{
provide free entry;
provide free food;
}
else
{
provide only free entry;
}
}
else
{
statement-block;
}
Nested if else
Animation of How nested if else
Works
Else if ladder Statement
 The word ladder means the staircase. As the name implies
this statement is used to choose right way/paths among
multiple paths.
 There is another way of putting if conditions together when
multiway decisions are involved.
 A multiway decision is a chain of if conditions in which the
statement associated with an else condition behaves like
another if condition.
 Else if ladder is also called 3 way or multiway decision
making statement.
Else if ladder- Structure
 if (test_condition 1)
statement-1;
else if (test_condition 2)
statement-2;
else if (test_condition 3)
statement-3;
else if (test_condition 4)
statement-4;
…………………………………..
else if (test_condition n)
statement-n;
statement-x;
Else if ladder- Example
if(Mark>=50 && Mark<60)
{ printf("Your grade is D");
}
else if(Mark>=60 && Mark<70)
{ printf("Your grade is C n");
}
else if(Mark>=70 && Mark<80)
{ printf("Your grade is B n");
}
else if(Mark>=80 && Mark<90)
{ printf("Your grade is A n");
}
else
printf("you have failed");
Animation of How else if ladder
Works
Switch Statement
 When one of the many statements is to be selected, then if
conditional statement can be used to control the selection.
 However the complexity of such a program increases
dramatically when the number of statements increases.
 Fortunately, C has a built in multiway decision making
statement known as switch.
 The switch statement tests the value of a given variable or
expression against a list of case values and when a match is
found only then a block of statements associated with that
case is executed.
 The general form is given below:
Switch Statement-Structure
switch(expression/ value)
{
case value-1:
statement-block-1;
break;
case value-2:
statement-block-2;
break;
……………………….
case value-n:
statement-block-n;
break;
default:
default-statement-block;
break;
}
statement-x;
Animation of How switch Works
Rules for Switch Statement
 The switch statement must be an integral type.
 Case labels must be constant or constant expression.
 Case labels must be unique. No two labels can have the same value.
 Case labels must end with colon.
 The break statement transfer the control out of the switch
statement.
 The break statement is optional. So two or more case labels may
belong to the same statements.
 The default label is optional. If present, it will be executed when
the expression does not find a matching case label.
 There can be at most one default label.
 The default may be placed any where but usually placed at the
end.
 It is permitted to nest switch statements.
Conditional Operator
 The C language has an unusual operator which is useful for
making two way decisions.
 This operator is a combination of ? and :
 It takes three operands. This operator is popularly known
as the conditional operator.
 The conditional operator can be used as the replacement of
if else conditional statement for two way decision making.
Conditional Operator
 The general structure of conditional operator:
Conditional expression? true-statement 1: false-statement;
 The condition is evaluated first. If the result is true then
the statement 1 is executed and its value is returned.
 Otherwise statement 2 is executed and its value is
returned.
 Example:
flag = (x<0) ? 0 :1;
Conditional Operator
Conditional operator:
flag = (x<0) ? 0 :1;
It is equivalent to:
if(x<0)
flag=0;
else
flag=1;
Questions?
References
 The C Programming Language (9780131103627): Brian W.
Kernighan, Dennis M. Ritchie
 C Programming: A Modern Approach, 2nd Edition By K. N.
King
 Absolute Beginner’s Guide To C, 2nd Edition By Greg Perry
42
Md. Shakhawat Hossain
Student of Department of Computer Science &
Engineering
University of Rajshahi
E-mail: mshimul86@gmail.com

More Related Content

What's hot

C programming decision making
C programming decision makingC programming decision making
C programming decision makingSENA
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in CMuthuganesh S
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c languagesneha2494
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programmingprogramming9
 
C++ decision making
C++ decision makingC++ decision making
C++ decision makingZohaib Ahmed
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programmingavikdhupar
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming LanguageMahantesh Devoor
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement Bharat Rathore
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in CJeya Lakshmi
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativityDr.Sandhiya Ravi
 

What's hot (20)

C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Branching in C
Branching in CBranching in C
Branching in C
 
C if else
C if elseC if else
C if else
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Input output statement in C
Input output statement in CInput output statement in C
Input output statement in C
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Break and continue
Break and continueBreak and continue
Break and continue
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Operator precedence and associativity
Operator precedence and associativityOperator precedence and associativity
Operator precedence and associativity
 

Similar to Decision making and branching

Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlanDeepak Lakhlan
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)TejaswiB4
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in CRAJ KUMAR
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c languagechintupro9
 
Decision control structures
Decision control structuresDecision control structures
Decision control structuresRahul Bathri
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
C language control statements
C language  control statementsC language  control statements
C language control statementssuman Aggarwal
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETUjwala Junghare
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlENGWAU TONNY
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)Papon Sarker
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrolsteach4uin
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
C statements
C statementsC statements
C statementsAhsann111
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, LoopingMURALIDHAR R
 

Similar to Decision making and branching (20)

Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
C statements.ppt presentation in c language
C statements.ppt presentation in c languageC statements.ppt presentation in c language
C statements.ppt presentation in c language
 
Decision control structures
Decision control structuresDecision control structures
Decision control structures
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
C language control statements
C language  control statementsC language  control statements
C language control statements
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
 
Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)
 
Selection statements
Selection statementsSelection statements
Selection statements
 
Cprogrammingprogramcontrols
CprogrammingprogramcontrolsCprogrammingprogramcontrols
Cprogrammingprogramcontrols
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
C statements
C statementsC statements
C statements
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 

More from Hossain Md Shakhawat (20)

Recipe for the effective presentaion
Recipe for the effective presentaionRecipe for the effective presentaion
Recipe for the effective presentaion
 
The Road to Higher study in Japan
The Road to Higher study in JapanThe Road to Higher study in Japan
The Road to Higher study in Japan
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
 
Islamic jurisprudence
Islamic jurisprudenceIslamic jurisprudence
Islamic jurisprudence
 
Introduction to Medical Imaging
Introduction to Medical ImagingIntroduction to Medical Imaging
Introduction to Medical Imaging
 
Jpeg compression
Jpeg compressionJpeg compression
Jpeg compression
 
Surah Fatiha
Surah FatihaSurah Fatiha
Surah Fatiha
 
Hashing
HashingHashing
Hashing
 
Digital signature
Digital signatureDigital signature
Digital signature
 
Caesar cipher
Caesar cipherCaesar cipher
Caesar cipher
 
Rsa rivest shamir adleman
Rsa rivest shamir adlemanRsa rivest shamir adleman
Rsa rivest shamir adleman
 
Fundamentals of cryptography
Fundamentals of cryptographyFundamentals of cryptography
Fundamentals of cryptography
 
Introduction to programming with c,
Introduction to programming with c,Introduction to programming with c,
Introduction to programming with c,
 
Introduction to digital image processing
Introduction to digital image processingIntroduction to digital image processing
Introduction to digital image processing
 
History of computing
History of computingHistory of computing
History of computing
 
Introduction to Printers
Introduction to PrintersIntroduction to Printers
Introduction to Printers
 
Input devices_(Mouse and Keyboard)
Input devices_(Mouse and Keyboard)Input devices_(Mouse and Keyboard)
Input devices_(Mouse and Keyboard)
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computer
 

Recently uploaded

What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxCeline George
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfcupulin
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Pharmaceutical Biotechnology VI semester.pdf
Pharmaceutical Biotechnology VI semester.pdfPharmaceutical Biotechnology VI semester.pdf
Pharmaceutical Biotechnology VI semester.pdfBALASUNDARESAN M
 

Recently uploaded (20)

What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Pharmaceutical Biotechnology VI semester.pdf
Pharmaceutical Biotechnology VI semester.pdfPharmaceutical Biotechnology VI semester.pdf
Pharmaceutical Biotechnology VI semester.pdf
 

Decision making and branching

  • 2.
  • 3. Introduction  “Decision making and branching” is one of the most important concepts of computer programming.  Programs should be able to make logical (true/false) decisions based on the condition provided.  Every program has one or few problems to solve. In order to solve those particular problems important decisions have to be made depending on the nature of the problems.  Generally C program execute it’s statements sequentially. But in order to solve problems we may have some situations where we have to change the order of executing the statements based on whether some conditions have met or not. So controlling the execution of statements based on certain condition or decision is called decision making and branching.
  • 4. START Read User Input Condition ? Perform Some Tasks Perform Some Other Tasks Show Result END Making Decision Condition not Satisfied [False] Condition Satisfied [True] Branch 2Branch 1 Branching
  • 5. Holiday Trip Problem  Consider the fact that you and some of your friends have planed to go out for a holiday trip after the Spring Semester, 2014.  You have also decided that if you have got received money 10000 taka or more from your parent then your will go out for a foreign trip. Otherwise, if the allotted money is less than 10000 then you will go out for a country side trip.  Now you are supposed to design a program to solve this problem.
  • 6. START Money Received 15000 Received Money>=10000 ? Holiday Enjoyed END Condition not Satisfied [False] Condition Satisfied [True] Holiday Trip Problem
  • 7. Decision Making & Branching in C  C language possesses decision making and branching capabilities by supporting the following statements: 1. If statement 2. Switch statement 3. Conditional operator statement 4. goto statement  These statements are knows as decision making statements. They are also called control statements as the control the flow of execution.
  • 8. IF Statement  The if statement is a powerful statement for decision making and is used to control the flow of execution of statements. It is basically a two-way decision making statement and is used in conjunction with an expression. It takes the following structure: if (test-condition)  It allows the computer to evaluate the expression first and then depending on whether the value of the expression or condition is true or false, it transfer the control to a particular statement. This point of program has two paths to follow, one for the true condition and the other for the false condition.
  • 10. IF Statement  The if statement can be implemented if four different forms depending on the complexity of the conditions to be tested. The four forms are: 1. Simple if statement 2. If else statement 3. Nested if else statement 4. Else if ladder
  • 11. Simple if Statement- Structure  The general form of a simple if statement is: if (test_condition) { statement-block; } statement x;
  • 12. Simple if Statement  The statement block may consists of a single statement or a group of statements.  If the test_condition is satisfied then the statement block will be executed first then the statement-x will be executed after completing the execution of statement block.  Otherwise if the test_condition doesn’t satisfied then, the statement block will be skipped and the execution will jump to the statement-x.  So in short when the condition is true then both the statement block and the statement-x are executed but in sequence.
  • 13. Simple if Statement- Flowchart Entry Test_Condition ? Statement-X; Statement-block; Next Statement; [ True ] [ False ]
  • 14. Simple if Statement  Important things to notice that if you want to control a single statement using the if condition then no need to create a block using the curly bracy.  The curly bracy is used to create statement block when it is necessary to include multiple statements under the same if condition.
  • 15. Simple if Statement-Example  Example with block of statement: if (marks>=90) { marks=marks+ bonus_marks; grade=“A+”; } printf(“The mark achieved:marks” , %d); Condition controlled statement
  • 16. Simple if Statement  Example of single statement: if (marks>=90) marks=marks+ bonus_marks; printf(“The mark achieved:marks” , %d); Condition controlled statement
  • 17. Simple if Statement  What happens if multiple statements follows after the if condition without using the curly brace?  Answer: Then the statement immediately following the if condition is considered as the controlling statement of the if condition. if the condition is true the controlled statement is executed otherwise not. Rest of the statements are executed sequentially.
  • 18. Simple if Statement  Example: if (marks>=90) marks=marks+ bonus_marks; grade=“A+”; printf(“The mark achieved:marks” , %d); Condition controlled statement
  • 19. Animation of How Simple if Works
  • 20. if else Statement  If the test condition is true then the true block statements, immediately following the if statements are executed;  Otherwise the false block statements are executed.  In short either true-block or false-block of statements will be executed, not both.  But in both cases the control is transferred subsequently to the statement-x as it is an independent (not controlled by the if else statement) statement.  It is also called two way conditional branching
  • 21. if else Statement- Structure  The if else statement is an extension of the simple if statement. The general form is : if (test_condition) { True block statements; } else { False block statements; } statement-x;
  • 22. if else Statement- Flowchart Entry Test_Condition ? Statement-X; True Statement-block; Next Statement; [ True ][ False ] False Statement-block;
  • 23. if else Statement- Example  Example with block of statement: if (marks>=40) { marks=marks+ bonus_marks; grade=“passed”; } else { marks=marks; grade=“failed”; } printf(“The mark achieved:marks” , %d); True block statement False block statement
  • 24. Animation of How if else Works
  • 25. Nested if else Statement  Using “if…else statement” within another “if…else statement” is called ‘nested if statement’. “Nested if statements” is mainly used to test multiple conditions.  It is called nested conditional branching.  They can be structured using following syntax:
  • 26. Nested if else Statement- Structure if (test_condition) { if (test_condition) { statement-block; } else { statement-block; } } else { statement-block; } statement-x; Nested if else
  • 27. Nested if else -Exampleif (gender==female) { if (age<10) { provide free entry; provide free food; } else { provide only free entry; } } else { statement-block; } Nested if else
  • 28. Animation of How nested if else Works
  • 29. Else if ladder Statement  The word ladder means the staircase. As the name implies this statement is used to choose right way/paths among multiple paths.  There is another way of putting if conditions together when multiway decisions are involved.  A multiway decision is a chain of if conditions in which the statement associated with an else condition behaves like another if condition.  Else if ladder is also called 3 way or multiway decision making statement.
  • 30. Else if ladder- Structure  if (test_condition 1) statement-1; else if (test_condition 2) statement-2; else if (test_condition 3) statement-3; else if (test_condition 4) statement-4; ………………………………….. else if (test_condition n) statement-n; statement-x;
  • 31. Else if ladder- Example if(Mark>=50 && Mark<60) { printf("Your grade is D"); } else if(Mark>=60 && Mark<70) { printf("Your grade is C n"); } else if(Mark>=70 && Mark<80) { printf("Your grade is B n"); } else if(Mark>=80 && Mark<90) { printf("Your grade is A n"); } else printf("you have failed");
  • 32. Animation of How else if ladder Works
  • 33. Switch Statement  When one of the many statements is to be selected, then if conditional statement can be used to control the selection.  However the complexity of such a program increases dramatically when the number of statements increases.  Fortunately, C has a built in multiway decision making statement known as switch.  The switch statement tests the value of a given variable or expression against a list of case values and when a match is found only then a block of statements associated with that case is executed.  The general form is given below:
  • 34. Switch Statement-Structure switch(expression/ value) { case value-1: statement-block-1; break; case value-2: statement-block-2; break; ………………………. case value-n: statement-block-n; break; default: default-statement-block; break; } statement-x;
  • 35. Animation of How switch Works
  • 36. Rules for Switch Statement  The switch statement must be an integral type.  Case labels must be constant or constant expression.  Case labels must be unique. No two labels can have the same value.  Case labels must end with colon.  The break statement transfer the control out of the switch statement.  The break statement is optional. So two or more case labels may belong to the same statements.  The default label is optional. If present, it will be executed when the expression does not find a matching case label.  There can be at most one default label.  The default may be placed any where but usually placed at the end.  It is permitted to nest switch statements.
  • 37. Conditional Operator  The C language has an unusual operator which is useful for making two way decisions.  This operator is a combination of ? and :  It takes three operands. This operator is popularly known as the conditional operator.  The conditional operator can be used as the replacement of if else conditional statement for two way decision making.
  • 38. Conditional Operator  The general structure of conditional operator: Conditional expression? true-statement 1: false-statement;  The condition is evaluated first. If the result is true then the statement 1 is executed and its value is returned.  Otherwise statement 2 is executed and its value is returned.  Example: flag = (x<0) ? 0 :1;
  • 39. Conditional Operator Conditional operator: flag = (x<0) ? 0 :1; It is equivalent to: if(x<0) flag=0; else flag=1;
  • 41. References  The C Programming Language (9780131103627): Brian W. Kernighan, Dennis M. Ritchie  C Programming: A Modern Approach, 2nd Edition By K. N. King  Absolute Beginner’s Guide To C, 2nd Edition By Greg Perry
  • 42. 42 Md. Shakhawat Hossain Student of Department of Computer Science & Engineering University of Rajshahi E-mail: mshimul86@gmail.com