SlideShare a Scribd company logo
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

PPTX
C programming
PPTX
Session04 selection structure_b
PPTX
Module 2- Control Structures
PPTX
Decision control
PPTX
control structure
PPTX
Control Structures, If..else, switch..case.pptx
PDF
Module 2 - Control Structures c programming.pptm.pdf
PPT
3. control statements
C programming
Session04 selection structure_b
Module 2- Control Structures
Decision control
control structure
Control Structures, If..else, switch..case.pptx
Module 2 - Control Structures c programming.pptm.pdf
3. control statements

Similar to Session04 selection structure_a (20)

PPTX
Ch05-converted.pptx
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
PPTX
BLM101_2.pptx
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PDF
PPSX
Bsit1
PPTX
Selection statements on programming with C
PPTX
Session05 iteration structure
PPT
03a control structures
PDF
conditional_statement.pdf
PPTX
control statements of clangauge (ii unit)
PPTX
Control Structures.pptx
PPTX
intro to CONTROL STRUCTURES 1 for C++.pptx
PPT
Visula C# Programming Lecture 3
PPT
The Three Basic Selection Structures in C++ Programming Concepts
PPTX
unit 2-Control Structures.pptx
PPTX
Constructs (Programming Methodology)
PPTX
Ch # 11 2nd year computer science notes1
PPTX
9. control statement
PPTX
NRG 106_Session 4_CLanguage.pptx
Ch05-converted.pptx
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
BLM101_2.pptx
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
Bsit1
Selection statements on programming with C
Session05 iteration structure
03a control structures
conditional_statement.pdf
control statements of clangauge (ii unit)
Control Structures.pptx
intro to CONTROL STRUCTURES 1 for C++.pptx
Visula C# Programming Lecture 3
The Three Basic Selection Structures in C++ Programming Concepts
unit 2-Control Structures.pptx
Constructs (Programming Methodology)
Ch # 11 2nd year computer science notes1
9. control statement
NRG 106_Session 4_CLanguage.pptx
Ad

More from HarithaRanasinghe (20)

PDF
annual financial report prime lands 2023/2024
PPTX
Asking Scientific Questions biointrractive
PPTX
Session12 pointers
PPTX
Session11 single dimarrays
PPTX
Session09 multi dimarrays
PPTX
Session07 recursion
PPTX
Session06 functions
PPTX
Session03 operators
PPT
Session02 c intro
PPT
Session01 basics programming
PPT
Program flow charts
PDF
Sad -sample_paper
PDF
Sad sample paper - mcq answers
PDF
Model questions
PDF
Model paper algorithms and data structures
PDF
Doc 20180208-wa0001
PDF
Doc 20180130-wa0006
PDF
Doc 20180130-wa0005
PDF
Doc 20180130-wa0004-1
annual financial report prime lands 2023/2024
Asking Scientific Questions biointrractive
Session12 pointers
Session11 single dimarrays
Session09 multi dimarrays
Session07 recursion
Session06 functions
Session03 operators
Session02 c intro
Session01 basics programming
Program flow charts
Sad -sample_paper
Sad sample paper - mcq answers
Model questions
Model paper algorithms and data structures
Doc 20180208-wa0001
Doc 20180130-wa0006
Doc 20180130-wa0005
Doc 20180130-wa0004-1
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation theory and applications.pdf
Spectroscopy.pptx food analysis technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Review of recent advances in non-invasive hemoglobin estimation
Programs and apps: productivity, graphics, security and other tools
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Session04 selection structure_a