SlideShare a Scribd company logo
DECISION MAKING AND
BRANCHING
3-May-20 Decision making branching 1
DECISION MAKING…
3-May-20 Decision making branching 2
• Decision making is about deciding the order of
execution of statements based on certain
conditions or repeat a group of statements
until certain specified conditions are met.
• Since they control the flow of statements also
called as Control Statements
• Two types:
- Conditional control statements
-unconditional control statements
Decision Making -Examples
• C language handles decision-making by supporting
the following statements:
Conditional Statements are:
if statement
switch statement
conditional operator statement (? : operator)
Unconditional Statements are:
goto statement
3-May-20 Decision making branching 3
DECISION MAKING WITH ‘IF’
- If is powerful decision making statement hence
called as two way decision making statement
Syntax:
If(test expression);
3-May-20 Decision making branching 4
False
DECISION MAKING WITH ‘IF’
- The if statement may be implemented in different
forms depending on the complexity of conditions to
be tested. The different forms are
• Simple if statement
• if....else statement
• Nested if....else statement
• Using else if statement
3-May-20 Decision making branching 5
SIMPLE ‘IF’
- The statements inside the body of “if” execute if
the given condition returns true.
- If the condition returns false then the statements
inside “if” are skipped.
- If condition is true both the block of statements
and next statement is executed
- The statement-block may be single or block of
statements
3-May-20 Decision making branching 6
SIMPLE ‘IF’
SYNTAX FLOWCHART
3-May-20 Decision making branching 7
if (test condition)
{
block of
statements;
... ... ...
}
statement-x;
SIMPLE ‘IF’
Program:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 13;
if (x > y )
{
printf("x is greater than y"); }}
O/P:
3-May-20 Decision making branching 8
x is greater than y
‘IF… ELSE’ STATEMENT
- It consists of two blocks of statements each enclosed
inside if block and else block
- If the condition is true, statements True block statement
will be executed
- otherwise statements inside else block are executed.
- In either case, only one block is executed ,not both .
3-May-20 Decision making branching 9
‘IF… ELSE’ STATEMENT
Syntax FLOWCHART
3-May-20 Decision making branching 10
if (test condition)
{
true block statements;
... ... ...
}
else
{
false block statements;
... ... ...
}
statement x;
Example: If… else Statement
Program
#include<stdio.h>
void main()
{
int n;
printf("Enter a number:");
scanf("%d",&n);
if(n%2 == 0)
printf("%d is even",n);
else
printf("%d is odd",n);
getch();
}
Output:
Enter a number: 3
3 is odd
3-May-20 Decision making branching 11
NESTING OF ‘IF … ELSE’ STATEMENT
- executes two different codes depending upon
whether the test expression is true or false.
- Sometimes, a choice has to be made from more
than 2 possibilities.
- allows to check for multiple test expressions and
execute different codes for more than two
conditions.
3-May-20 Decision making branching 12
Syntax: Nested if else
if(test condition1)
{
If(test condition2)
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
statement –x;
3-May-20 Decision making branching 13
3-May-20 Decision making branching 14
Flowchart: Nested if… else
Example: Nested if…else
Program:
void main()
{
float a,b,c;
printf(“ Enter Three values”);
scanf(“%f%f%f”, &a,&b,&c);
printf(“largest value is”);
if(a>b)
{
if(a>c)
printf(“%fn”,a)
else
printf(“%fn”,c)
}
else
{
if(c>b)
printf(“%fn”,c)
else
printf(“%fn”,b)
}
getch();
}
Output:
Enter Three values
22.22 87.43 67.45
Largest value is 87.43
3-May-20 Decision making branching 15
else…if Ladder
- to test set of conditions in sequence called as
multipath decisions
- statements are evaluated from top to bottom
- As soon as the true statement is found, the
statement associated with it is executed
- the control is transferred to the statement-x
(Skipping the rest of the conditions)
- when all the ‘n’ conditions become false, the
final else, containing the default-statement will
be executed3-May-20 Decision making branching 16
Syntax: else…if Ladder
if(condition-1)
{
statements;
}
else if
(condition-2)
{
statements;
}
else if
(condition-3)
{
statements;
}
:
:
else
{
statements;
}3-May-20 Decision making branching 17
3-May-20 Decision making branching 18
Example: else…if Ladder
#include<stdio.h>
void main()
{
int mark;
printf(“Enter Mark:”);
scanf(“%d”, &mark);
if(mark= = 100)
{
printf(“Centum Result”);
}
else if
if((mark >=75 )&&(mark<=99)
{
printf(“Merit Result”);
}
else if
if((mark>=60 )&&(mark<=75)
{
printf(“First Class Result”);
}
else if
if(mark>=40 )&&(mark<=59)
{
printf(“Pass Result”);
}
else
printf(“Fail Result”);
}
getch();
}
3-May-20 Decision making branching 19
Output:
Enter Marks: 79
First Class Result
3-May-20 Decision making branching 20
SWITCH CASE
- Switch is also called as multiway decision statement
- allows to choose only one choice among the many
given choices
- allows a variable to be tested for equality against a list
of values
- The expression in switch evaluates to return an
integral value, which is then compared to the values
present in different cases.
- It executes that block of code which matches the case
value.
- If there is no match, then default block is executed (if
present)3-May-20 Decision making branching 21
SYNTAX: SWITCH CASEswitch(expression)
{
case value-1 :
block-1;
break; /* optional */
case value-2 :
block-2;
break; /* optional */
case value-n :
block-n;
break; /* optional */
:
default :
default-block;
break; /* Optional */
}
3-May-20 Decision making branching 22
SWITCH CASE
- The expression is an integer expression or
characters
- value-1,value-2 are constants or constant
expression also called as case labels
- block1,block2 are statement lists may contain zero
or more statements
- Case labels end with colon(:)
- Break statement at the end of each block signals
the end of particular case and cause an exit from
switch
- Default is an optional case3-May-20 Decision making branching 23
3-May-20 Decision making branching 24
3-May-20 Decision making branching 25
Rules for SWITCH Statement
- The switch expression must be an integral type
- Case labels must be constant or constant
expression.
- Case labels must be unique end with colon(:)
- Break statement transfers the control out of the
switch statement
- Break and default is an optional.
- There can be atmost one default label
- Default may be placed anywhere but usually
placed at the end
3-May-20 Decision making branching 26
The ?: operator
- The conditional operator (? :) is a ternary
operator(it takes three operands), makes two-way
decisions.
- Conditional operator is closely related
with if..else statement.
- If the condition is true then expression1 is
executed else expression2 is executed.3-May-20 Decision making branching 27
The ?: operator
Program:
#include <stdio.h>
int main()
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
puts(mark >= 40 ? "Passed" :
"Failed");
return 0;
}
Output
Enter mark:
39 Failed
Explanation
The program checks the
condition mark >=40, if it is
true "Passed" is printed
else "Failed".
3-May-20 Decision making branching 28
GOTO STATEMENT
- Got statement is used to branch unconditionally
from one point to another in the program
- the control jumps directly to the label mentioned
in the ‘goto’ statement
- A label is an identifier required for ‘goto’
statement to a place where the branch is to be
made
- Label can be anywhere in the program either
before or after the goto label
- It breaks the normal sequence of the program
3-May-20 Decision making branching 29
GOTO STATEMENT
Defining a label: label_name:
- label_name should be a valid identifier name.
- : (colon) should be used after the label_name.
-If the label: is before the statement goto label;
a loop will be formed and some statements will be
executed repeatedly called as Backward jump
-If the label: is placed after the goto label;
some statements will be skipped and the jump is
known as forward jump
3-May-20 Decision making branching 30
SYNTAX: GOTO STATEMENT
Forward jump
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
Backward Jump
label:
statement;
... .. ...
... .. ...
... .. ...
goto label;
3-May-20 Decision making branching 31
;
Flowchart of gotostatemet
3-May-20 Decision making branching 32
EXAMPLE: BackwardGOTO STATEMENT
include <stdio.h>
int main()
{
int number;
number=1;
repeat:
printf("%dn",number);
number++;
if(number<=10)
goto repeat;
return 0;
}
Output:
1
2
3
4
5
6
7
8
9
10
3-May-20 Decision making branching 33
EXAMPLE: GOTO STATEMENT
#include <stdio.h>
void main()
{
int number;
printf("Enter an integer number: ");
scanf("%d",&number);
if(number<=0)
goto end;
printf("Number is : %d", number);
end: printf("Bye Bye !!!");
}
Output:
First run:
Enter an integer
number: 123
Number is : 123
Bye Bye !!!
Second run:
Enter an integer
number: 0
Bye Bye !!!
3-May-20 Decision making branching 34
MAHENDIRAN N
ASSISTANT PROFESSOR
Department of Computer Science
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
35
Ad

More Related Content

What's hot (20)

ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
Srinath Maharana
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
SENA
 
Ddl &amp; dml commands
Ddl &amp; dml commandsDdl &amp; dml commands
Ddl &amp; dml commands
AnjaliJain167
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
Rumman Ansari
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
I L0V3 CODING DR
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
While loop
While loopWhile loop
While loop
Feras_83
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Looping statement
Looping statementLooping statement
Looping statement
ilakkiya
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
Lakshman Basnet
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Oracle: Control Structures
Oracle: Control StructuresOracle: Control Structures
Oracle: Control Structures
DataminingTools Inc
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
SENA
 
Ddl &amp; dml commands
Ddl &amp; dml commandsDdl &amp; dml commands
Ddl &amp; dml commands
AnjaliJain167
 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
While loop
While loopWhile loop
While loop
Feras_83
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
Looping statement
Looping statementLooping statement
Looping statement
ilakkiya
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
tanmaymodi4
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
MANJUTRIPATHI7
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 

Similar to Decision makingandbranching in c (20)

C statements
C statementsC statements
C statements
Ahsann111
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statementsDecision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Decision Making.pptx
Decision Making.pptxDecision Making.pptx
Decision Making.pptx
AthulyaRamachandran2
 
Controls & Loops in C
Controls & Loops in C Controls & Loops in C
Controls & Loops in C
Thesis Scientist Private Limited
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
University of Potsdam
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
Praveen M Jigajinni
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
JavvajiVenkat
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Unit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdfUnit 2=Decision Control & Looping Statements.pdf
Unit 2=Decision Control & Looping Statements.pdf
Dr. Ambedkar Institute of Technology, Bangalore 56
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
Chap 5(decision making-branching)
Chap 5(decision making-branching)Chap 5(decision making-branching)
Chap 5(decision making-branching)
Bangabandhu Sheikh Mujibur Rahman Science and Technology University
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
C statements
C statementsC statements
C statements
Ahsann111
 
Decision making in C(2020-2021) statements
Decision making in C(2020-2021) statementsDecision making in C(2020-2021) statements
Decision making in C(2020-2021) statements
BalaKrishnan466
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
Rai University
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
Rai University
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
Rai University
 
Diploma ii cfpc u-3 handling input output and control statements
Diploma ii  cfpc u-3 handling input output and control statementsDiploma ii  cfpc u-3 handling input output and control statements
Diploma ii cfpc u-3 handling input output and control statements
Rai University
 
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdfPROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
PROBLEM SOLVING USING NOW PPSC- UNIT -2.pdf
JNTUK KAKINADA
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
Rai University
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
Dr. Chandrakant Divate
 
Visula C# Programming Lecture 3
Visula C# Programming Lecture 3Visula C# Programming Lecture 3
Visula C# Programming Lecture 3
Abou Bakr Ashraf
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Unit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment exampleUnit_3A_If_Else_Switch and a if else statment example
Unit_3A_If_Else_Switch and a if else statment example
umaghosal12101974
 
2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt2. Control structures with for while and do while.ppt
2. Control structures with for while and do while.ppt
ManojKhadilkar1
 
Ad

Recently uploaded (20)

Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.Open Access: Revamping Library Learning Resources.
Open Access: Revamping Library Learning Resources.
Rishi Bankim Chandra Evening College, Naihati, North 24 Parganas, West Bengal, India
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
Envenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptxEnvenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptx
rekhapositivity
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
Fundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic CommunicationsFundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic Communications
Jordan Williams
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Unit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theoriesUnit 5: Dividend Decisions and its theories
Unit 5: Dividend Decisions and its theories
bharath321164
 
Envenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptxEnvenomation---Clinical Toxicology. pptx
Envenomation---Clinical Toxicology. pptx
rekhapositivity
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd yearVitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
Vitamins Chapter-7, Biochemistry and clinical pathology, D.Pharm 2nd year
ARUN KUMAR
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Diabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomicDiabetic neuropathy peripheral autonomic
Diabetic neuropathy peripheral autonomic
Pankaj Patawari
 
Fundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic CommunicationsFundamentals of PR: Wk 4 - Strategic Communications
Fundamentals of PR: Wk 4 - Strategic Communications
Jordan Williams
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Ad

Decision makingandbranching in c

  • 1. DECISION MAKING AND BRANCHING 3-May-20 Decision making branching 1
  • 2. DECISION MAKING… 3-May-20 Decision making branching 2 • Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. • Since they control the flow of statements also called as Control Statements • Two types: - Conditional control statements -unconditional control statements
  • 3. Decision Making -Examples • C language handles decision-making by supporting the following statements: Conditional Statements are: if statement switch statement conditional operator statement (? : operator) Unconditional Statements are: goto statement 3-May-20 Decision making branching 3
  • 4. DECISION MAKING WITH ‘IF’ - If is powerful decision making statement hence called as two way decision making statement Syntax: If(test expression); 3-May-20 Decision making branching 4 False
  • 5. DECISION MAKING WITH ‘IF’ - The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are • Simple if statement • if....else statement • Nested if....else statement • Using else if statement 3-May-20 Decision making branching 5
  • 6. SIMPLE ‘IF’ - The statements inside the body of “if” execute if the given condition returns true. - If the condition returns false then the statements inside “if” are skipped. - If condition is true both the block of statements and next statement is executed - The statement-block may be single or block of statements 3-May-20 Decision making branching 6
  • 7. SIMPLE ‘IF’ SYNTAX FLOWCHART 3-May-20 Decision making branching 7 if (test condition) { block of statements; ... ... ... } statement-x;
  • 8. SIMPLE ‘IF’ Program: #include <stdio.h> void main( ) { int x, y; x = 15; y = 13; if (x > y ) { printf("x is greater than y"); }} O/P: 3-May-20 Decision making branching 8 x is greater than y
  • 9. ‘IF… ELSE’ STATEMENT - It consists of two blocks of statements each enclosed inside if block and else block - If the condition is true, statements True block statement will be executed - otherwise statements inside else block are executed. - In either case, only one block is executed ,not both . 3-May-20 Decision making branching 9
  • 10. ‘IF… ELSE’ STATEMENT Syntax FLOWCHART 3-May-20 Decision making branching 10 if (test condition) { true block statements; ... ... ... } else { false block statements; ... ... ... } statement x;
  • 11. Example: If… else Statement Program #include<stdio.h> void main() { int n; printf("Enter a number:"); scanf("%d",&n); if(n%2 == 0) printf("%d is even",n); else printf("%d is odd",n); getch(); } Output: Enter a number: 3 3 is odd 3-May-20 Decision making branching 11
  • 12. NESTING OF ‘IF … ELSE’ STATEMENT - executes two different codes depending upon whether the test expression is true or false. - Sometimes, a choice has to be made from more than 2 possibilities. - allows to check for multiple test expressions and execute different codes for more than two conditions. 3-May-20 Decision making branching 12
  • 13. Syntax: Nested if else if(test condition1) { If(test condition2) { statement block1; } else { statement block2; } } else { statement block3; } statement –x; 3-May-20 Decision making branching 13
  • 14. 3-May-20 Decision making branching 14 Flowchart: Nested if… else
  • 15. Example: Nested if…else Program: void main() { float a,b,c; printf(“ Enter Three values”); scanf(“%f%f%f”, &a,&b,&c); printf(“largest value is”); if(a>b) { if(a>c) printf(“%fn”,a) else printf(“%fn”,c) } else { if(c>b) printf(“%fn”,c) else printf(“%fn”,b) } getch(); } Output: Enter Three values 22.22 87.43 67.45 Largest value is 87.43 3-May-20 Decision making branching 15
  • 16. else…if Ladder - to test set of conditions in sequence called as multipath decisions - statements are evaluated from top to bottom - As soon as the true statement is found, the statement associated with it is executed - the control is transferred to the statement-x (Skipping the rest of the conditions) - when all the ‘n’ conditions become false, the final else, containing the default-statement will be executed3-May-20 Decision making branching 16
  • 17. Syntax: else…if Ladder if(condition-1) { statements; } else if (condition-2) { statements; } else if (condition-3) { statements; } : : else { statements; }3-May-20 Decision making branching 17
  • 18. 3-May-20 Decision making branching 18
  • 19. Example: else…if Ladder #include<stdio.h> void main() { int mark; printf(“Enter Mark:”); scanf(“%d”, &mark); if(mark= = 100) { printf(“Centum Result”); } else if if((mark >=75 )&&(mark<=99) { printf(“Merit Result”); } else if if((mark>=60 )&&(mark<=75) { printf(“First Class Result”); } else if if(mark>=40 )&&(mark<=59) { printf(“Pass Result”); } else printf(“Fail Result”); } getch(); } 3-May-20 Decision making branching 19
  • 20. Output: Enter Marks: 79 First Class Result 3-May-20 Decision making branching 20
  • 21. SWITCH CASE - Switch is also called as multiway decision statement - allows to choose only one choice among the many given choices - allows a variable to be tested for equality against a list of values - The expression in switch evaluates to return an integral value, which is then compared to the values present in different cases. - It executes that block of code which matches the case value. - If there is no match, then default block is executed (if present)3-May-20 Decision making branching 21
  • 22. SYNTAX: SWITCH CASEswitch(expression) { case value-1 : block-1; break; /* optional */ case value-2 : block-2; break; /* optional */ case value-n : block-n; break; /* optional */ : default : default-block; break; /* Optional */ } 3-May-20 Decision making branching 22
  • 23. SWITCH CASE - The expression is an integer expression or characters - value-1,value-2 are constants or constant expression also called as case labels - block1,block2 are statement lists may contain zero or more statements - Case labels end with colon(:) - Break statement at the end of each block signals the end of particular case and cause an exit from switch - Default is an optional case3-May-20 Decision making branching 23
  • 24. 3-May-20 Decision making branching 24
  • 25. 3-May-20 Decision making branching 25
  • 26. Rules for SWITCH Statement - The switch expression must be an integral type - Case labels must be constant or constant expression. - Case labels must be unique end with colon(:) - Break statement transfers the control out of the switch statement - Break and default is an optional. - There can be atmost one default label - Default may be placed anywhere but usually placed at the end 3-May-20 Decision making branching 26
  • 27. The ?: operator - The conditional operator (? :) is a ternary operator(it takes three operands), makes two-way decisions. - Conditional operator is closely related with if..else statement. - If the condition is true then expression1 is executed else expression2 is executed.3-May-20 Decision making branching 27
  • 28. The ?: operator Program: #include <stdio.h> int main() { int mark; printf("Enter mark: "); scanf("%d", &mark); puts(mark >= 40 ? "Passed" : "Failed"); return 0; } Output Enter mark: 39 Failed Explanation The program checks the condition mark >=40, if it is true "Passed" is printed else "Failed". 3-May-20 Decision making branching 28
  • 29. GOTO STATEMENT - Got statement is used to branch unconditionally from one point to another in the program - the control jumps directly to the label mentioned in the ‘goto’ statement - A label is an identifier required for ‘goto’ statement to a place where the branch is to be made - Label can be anywhere in the program either before or after the goto label - It breaks the normal sequence of the program 3-May-20 Decision making branching 29
  • 30. GOTO STATEMENT Defining a label: label_name: - label_name should be a valid identifier name. - : (colon) should be used after the label_name. -If the label: is before the statement goto label; a loop will be formed and some statements will be executed repeatedly called as Backward jump -If the label: is placed after the goto label; some statements will be skipped and the jump is known as forward jump 3-May-20 Decision making branching 30
  • 31. SYNTAX: GOTO STATEMENT Forward jump goto label; ... .. ... ... .. ... ... .. ... label: statement; Backward Jump label: statement; ... .. ... ... .. ... ... .. ... goto label; 3-May-20 Decision making branching 31 ;
  • 32. Flowchart of gotostatemet 3-May-20 Decision making branching 32
  • 33. EXAMPLE: BackwardGOTO STATEMENT include <stdio.h> int main() { int number; number=1; repeat: printf("%dn",number); number++; if(number<=10) goto repeat; return 0; } Output: 1 2 3 4 5 6 7 8 9 10 3-May-20 Decision making branching 33
  • 34. EXAMPLE: GOTO STATEMENT #include <stdio.h> void main() { int number; printf("Enter an integer number: "); scanf("%d",&number); if(number<=0) goto end; printf("Number is : %d", number); end: printf("Bye Bye !!!"); } Output: First run: Enter an integer number: 123 Number is : 123 Bye Bye !!! Second run: Enter an integer number: 0 Bye Bye !!! 3-May-20 Decision making branching 34
  • 35. MAHENDIRAN N ASSISTANT PROFESSOR Department of Computer Science Sri Ramakrishna College of Arts and Science Coimbatore - 641 006 Tamil Nadu, India 35