SlideShare a Scribd company logo
1 of 14
Control structures control the flow of execution in a program or
function.
There are three kinds of execution flow:
– Sequence:
• the execution of the program is sequential.
– Selection:
• A control structure which chooses alternative to
execute.
– Repetition:
• A control structure which repeats a group of
statements.
Control
Structures in C
A program may choose among alternative statements by testing the value
of key variables.
– e.g., if( your_grade > 60 )
printf(“you are passed!”);
Condition is an expression that is either false (represented by 0) or true
(represented by 1).
– e.g., “your_grade > 60” is a condition.
Conditions may contain relational or equality operators, and have the
following forms.
– variable relational-operator variable (or constant)
– variable equality-operator variable (or constant)
Conditions
Operator Meaning Type
< Less than Relational
> Greater than Relational
<= Less than or equal to Relational
>= Greater than or equal
to
Relational
== Equal to Equality
!= Not equal to Equality
Operators Used in Conditions
Operator Condition Meaning
<= x <= 0 x less than or
equal to 0
< Power < MAX_POW Power less than
MAX_POW
== mom_or_dad == ‘M’ mom_or_dad
equal to ‘M’
!= num != SETINEL num not equal
to SETINEL
Examples of Conditions
Logical
Operators
There are three kinds of
logical operators.
– &&: and
– ||: or
– !: not
Logical expression is an
expression which uses one or
more logical operators, e.g.,
– (temperature > 90.0 &&
humidity > 0.90)
– !(n <= 0 || n >= 100).
Op 1 Op 2 Op 1 && Op2 Op 1 || Op2
nonzero nonzero 1 1
nonzero 0 0 1
0 nonzero 0 1
0 0 0 0
Op 1 ! Op 1
nonzero 0
0 1
The Truth Table of Logical Operators
The if statement is the primary selection control structure.
Syntax: if (condition) statement;
else statement;
An example of two alternatives:
if ( rest_heart_rate > 56 )
printf(“Keep up your exercise program!n”);
else
printf(“Your heart is in excellent health!n”);
An example of one alternative:
if ( x != 0.0 )
product = product * x;
The if
Statement
Decision Decision
Flowcharts of if Statements with (a) Two
Alternatives and (b) One Alternative
Nested if statement is an if statement with
another if statement as its true task or false
task. e.g.,
if (road_status == ‘S’)
else
printf(“Drive carefully!n”);
if (temp > 0) {
printf(“Wet roads ahead!n”);
}else{
printf(“Icy roads ahead!n”);
}
Nested if Statements
Another if statement
Main if statement
An Example for the Flowchart of
Nested if Statements
If there are many alternatives, it is better to use the
syntax of multiple-alternative decision.
Syntax:
if (condition1)
statement1
else if (condition2)
statement2
…
else if (conditionn)
statementn
else
statemente
Multiple-
Alternative
Decisions
An Example of Multiple-Alternative
Decisions
The switch statement is used to select one of several
alternatives when the selection is based on the value of a single
variable or an expression. switch (controlling expression) {
case label1:
statement1
break;
case label2:
statement2
break;
…
case labeln:
statementn
break;
default:
statementd;
}
If the result of this controlling expression
matches label1, execute staement1 and then break
this switch block.
If the result matches none of all labels, execute t
default statementd.
The switch Statement
class is a char variable.
Two or more cases can execute the same
statement.
An Example of a switch Statement with
Type char Case Labels

More Related Content

Similar to Decision control

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
chintupro9
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
sshhzap
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
patcha535
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 

Similar to Decision control (20)

LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
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
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
The Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming ConceptsThe Three Basic Selection Structures in C++ Programming Concepts
The Three Basic Selection Structures in C++ Programming Concepts
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
control structure
control structurecontrol structure
control structure
 
Selection structure
Selection structureSelection structure
Selection structure
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
Bsit1
Bsit1Bsit1
Bsit1
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
M C6java5
M C6java5M C6java5
M C6java5
 

More from Dr.Subha Krishna (6)

Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Programming questions
Programming questionsProgramming questions
Programming questions
 
Programming qns
Programming qnsProgramming qns
Programming qns
 
Programming egs
Programming egs Programming egs
Programming egs
 
Functions
Functions Functions
Functions
 
C Tutorial
C TutorialC Tutorial
C Tutorial
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 

Recently uploaded (20)

Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Decision control

  • 1. Control structures control the flow of execution in a program or function. There are three kinds of execution flow: – Sequence: • the execution of the program is sequential. – Selection: • A control structure which chooses alternative to execute. – Repetition: • A control structure which repeats a group of statements. Control Structures in C
  • 2. A program may choose among alternative statements by testing the value of key variables. – e.g., if( your_grade > 60 ) printf(“you are passed!”); Condition is an expression that is either false (represented by 0) or true (represented by 1). – e.g., “your_grade > 60” is a condition. Conditions may contain relational or equality operators, and have the following forms. – variable relational-operator variable (or constant) – variable equality-operator variable (or constant) Conditions
  • 3. Operator Meaning Type < Less than Relational > Greater than Relational <= Less than or equal to Relational >= Greater than or equal to Relational == Equal to Equality != Not equal to Equality Operators Used in Conditions
  • 4. Operator Condition Meaning <= x <= 0 x less than or equal to 0 < Power < MAX_POW Power less than MAX_POW == mom_or_dad == ‘M’ mom_or_dad equal to ‘M’ != num != SETINEL num not equal to SETINEL Examples of Conditions
  • 5. Logical Operators There are three kinds of logical operators. – &&: and – ||: or – !: not Logical expression is an expression which uses one or more logical operators, e.g., – (temperature > 90.0 && humidity > 0.90) – !(n <= 0 || n >= 100).
  • 6. Op 1 Op 2 Op 1 && Op2 Op 1 || Op2 nonzero nonzero 1 1 nonzero 0 0 1 0 nonzero 0 1 0 0 0 0 Op 1 ! Op 1 nonzero 0 0 1 The Truth Table of Logical Operators
  • 7. The if statement is the primary selection control structure. Syntax: if (condition) statement; else statement; An example of two alternatives: if ( rest_heart_rate > 56 ) printf(“Keep up your exercise program!n”); else printf(“Your heart is in excellent health!n”); An example of one alternative: if ( x != 0.0 ) product = product * x; The if Statement
  • 8. Decision Decision Flowcharts of if Statements with (a) Two Alternatives and (b) One Alternative
  • 9. Nested if statement is an if statement with another if statement as its true task or false task. e.g., if (road_status == ‘S’) else printf(“Drive carefully!n”); if (temp > 0) { printf(“Wet roads ahead!n”); }else{ printf(“Icy roads ahead!n”); } Nested if Statements
  • 10. Another if statement Main if statement An Example for the Flowchart of Nested if Statements
  • 11. If there are many alternatives, it is better to use the syntax of multiple-alternative decision. Syntax: if (condition1) statement1 else if (condition2) statement2 … else if (conditionn) statementn else statemente Multiple- Alternative Decisions
  • 12. An Example of Multiple-Alternative Decisions
  • 13. The switch statement is used to select one of several alternatives when the selection is based on the value of a single variable or an expression. switch (controlling expression) { case label1: statement1 break; case label2: statement2 break; … case labeln: statementn break; default: statementd; } If the result of this controlling expression matches label1, execute staement1 and then break this switch block. If the result matches none of all labels, execute t default statementd. The switch Statement
  • 14. class is a char variable. Two or more cases can execute the same statement. An Example of a switch Statement with Type char Case Labels