SlideShare a Scribd company logo
1 of 17
Control Structure
• Sequence structures
• Built into C
• Programs executed sequentially by default
• Selection structures
• C has three types: if, if-else, and switch
• Repetition structures
• C has three types: while, do/while and for
Sequence Structure
Sequence Structure
• Programs executed sequentially by default
Control flow:
• Statements executed one after the other in the order written
statement 1
statement 2
Sequence Structure
The if Selection Structure
False
Print “Passed”
grade >= 60
True
if Selection Structure
if (grade>=60)
printf(“Passed”);
0(zero):false
1(nonzero):true
The if-else Selection Structure
Print “Passed”
True
Print “Failed”
grade >= 60
False
if-else Selection Structure
if (grade>=60)
printf(“Passed”);
else
printf(“Failed”);
Conditional Operator
? :
<expression-1>?<expression-2>:<expression-3>
printf(“%s”,(grade>=60)? “Passed”:“Failed”);
(grade>=60)?printf(“Passed”):printf(“Failed”);
Nested Selection Structure
Nested if-else Structure
if (grade>=90)
printf(“A”);
else
if (grade>=80)
printf(“B”);
else
if (grade>=80)
printf(“C”);
else
if (grade>=60)
printf(“D”);
else
printf(“E”);
if (grade>=90)
printf(“A”);
else if (grade>=80)
printf(“B”);
else if (grade>=80)
printf(“C”);
else if (grade>=60)
printf(“D”);
else
printf(“E”);
Compound Statement Nested In if/else
if (grade>=60)
printf(“Passed”);
else {
printf(“Failedn”);
printf(“You must take the course again!”);
}
Two lines
Logical Calculation
(y>5)&&(y<10)
(x<-10)||(x>0)
• !(logical NOT)
• &&(logical AND)
• ||(logical OR)
Logical Expression
(x>5)||(x<-5)
!(a<b)&&(m!=n)
(a+b>c)&&(a+c>b)&&(b+c>a)
Logical Calculation
a !a
1 0
0 1
a b a&&b
1 1 1
1 0 0
0 1 0
0 0 0
a b a||b
1 1 1
1 0 1
0 1 1
0 0 0
!
&&
||
Use Of Logical Expression
void main() {
char c;
c=getchar();
if ((c>=‘A’&&c<=‘Z’)||(c>=‘a’&&c<=‘z’))
printf(“%c is a letter.”, c);
else if (c>=‘0’&&c<=‘9’)
printf(“%c is a digit.”, c);
else
printf(“%c is neither a letter nor a digit.”, c);
}
Multiple-Selection Structure
switch
switch(expression)
{ case <constant_expression_1>: action_1; [break;]
case <constant_expression_2>: action_2; [break;]
……
case <constant_expression_n-1>:action_n-1; [break;]
[default: action_n; [break;]]
}
Multiple-Selection Structure
grade=getchar();
switch(grade)
{ case ‘A’: printf(“85~100n”);
case ‘B’: printf(“70~84n”);
case ‘C’: printf(“60~69n”);
case ‘D’: printf(“<60n”);
default: printf(“errorn”);
}
A
85~100
70~84
60~69
<60
error
Multiple-Selection Structure(cont.)
print
“60~69”
print
“70~84”
print
“85~100”
print
“<60”
print
“error”
‘A’ ‘B’ ‘C’ ‘D’ default
grade
Multiple-Selection Structure
print
“60~69”
print
“70~84”
print
“85~100”
print
“<60”
print
“error”
‘A’ ‘B’ ‘C’ ‘D’ default
grade
Multiple-Selection Structure(cont.)
switch
switch(grade)
{ case ‘A’: printf(“85~100n”); break;
case ‘B’: printf(“70~84n”); break;
case ‘C’: printf(“60~69n”); break;
case ‘D’: printf(“<60n”); break;
default: printf(“errorn”);
}
85~100
break
Multiple-Selection Structure
#include <stdio.h>
int main()
{
char grade;
printf("Enter a grade A-D ");
scanf("%c",&grade);
switch(grade)
{
case 'A': printf("85 ~ 100n"); break;
case 'B': printf("70 ~ 84n"); break;
case 'C': printf("60 ~ 69n"); break;
case 'D': printf("<60n"); break;
default: printf("errorn");
}
}

More Related Content

Similar to Session04 selection structure_a

control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)Prashant Sharma
 
Lecture#3 Algorithms and computing
Lecture#3 Algorithms and computingLecture#3 Algorithms and computing
Lecture#3 Algorithms and computingNUST Stuff
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptSuleman Khan
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control StructuresPRN USM
 
Chapter 4.4
Chapter 4.4Chapter 4.4
Chapter 4.4sotlsoc
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control StatementsSiddharthSelenium
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Ahmad Bashar Eter
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement OXUS 20
 
Unit2 control statements
Unit2 control statementsUnit2 control statements
Unit2 control statementsdeepak kumbhar
 
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 exampleumaghosal12101974
 

Similar to Session04 selection structure_a (17)

Chapter 1 Nested Control Structures
Chapter 1 Nested Control StructuresChapter 1 Nested Control Structures
Chapter 1 Nested Control Structures
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
Lecture#3 Algorithms and computing
Lecture#3 Algorithms and computingLecture#3 Algorithms and computing
Lecture#3 Algorithms and computing
 
Decision control
Decision controlDecision control
Decision control
 
conditional_statement.pdf
conditional_statement.pdfconditional_statement.pdf
conditional_statement.pdf
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
cpphtp4_PPT_02.ppt
cpphtp4_PPT_02.pptcpphtp4_PPT_02.ppt
cpphtp4_PPT_02.ppt
 
Chapter 4.4
Chapter 4.4Chapter 4.4
Chapter 4.4
 
Bsit1
Bsit1Bsit1
Bsit1
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Session 07 - Flow Control Statements
Session 07 - Flow Control StatementsSession 07 - Flow Control Statements
Session 07 - Flow Control Statements
 
Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1Acm aleppo cpc training introduction 1
Acm aleppo cpc training introduction 1
 
Conditional Statement
Conditional Statement Conditional Statement
Conditional Statement
 
Unit2 control statements
Unit2 control statementsUnit2 control statements
Unit2 control statements
 
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
 

More from HarithaRanasinghe (20)

Session12 pointers
Session12 pointersSession12 pointers
Session12 pointers
 
Session11 single dimarrays
Session11 single dimarraysSession11 single dimarrays
Session11 single dimarrays
 
Session09 multi dimarrays
Session09 multi dimarraysSession09 multi dimarrays
Session09 multi dimarrays
 
Session07 recursion
Session07 recursionSession07 recursion
Session07 recursion
 
Session06 functions
Session06 functionsSession06 functions
Session06 functions
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structure
 
Session04 selection structure_b
Session04 selection structure_bSession04 selection structure_b
Session04 selection structure_b
 
Session03 operators
Session03 operatorsSession03 operators
Session03 operators
 
Session02 c intro
Session02 c introSession02 c intro
Session02 c intro
 
Session01 basics programming
Session01 basics programmingSession01 basics programming
Session01 basics programming
 
Program flow charts
Program flow chartsProgram flow charts
Program flow charts
 
Sad -sample_paper
Sad  -sample_paperSad  -sample_paper
Sad -sample_paper
 
Sad sample paper - mcq answers
Sad   sample paper - mcq answersSad   sample paper - mcq answers
Sad sample paper - mcq answers
 
Paper
PaperPaper
Paper
 
Model questions
Model questionsModel questions
Model questions
 
Model paper algorithms and data structures
Model paper  algorithms and data structuresModel paper  algorithms and data structures
Model paper algorithms and data structures
 
Doc 20180208-wa0001
Doc 20180208-wa0001Doc 20180208-wa0001
Doc 20180208-wa0001
 
Doc 20180130-wa0006
Doc 20180130-wa0006Doc 20180130-wa0006
Doc 20180130-wa0006
 
Doc 20180130-wa0005
Doc 20180130-wa0005Doc 20180130-wa0005
Doc 20180130-wa0005
 
Doc 20180130-wa0004-1
Doc 20180130-wa0004-1Doc 20180130-wa0004-1
Doc 20180130-wa0004-1
 

Recently uploaded

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....rightmanforbloodline
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseWSO2
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceIES VE
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Recently uploaded (20)

Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

Session04 selection structure_a