SlideShare a Scribd company logo
Control Structure continued….
• Selection structures
• C has three types: if, if-else, and switch
• Repetition structures
• C has three types: while, do/while and for
Different forms of ‘if’ structures
If structure If and else structure If and else if structure
If(condition)
//…………………….
If(condition)
//…………………….
else
//……………………..
If(condition1)
//…………………….
else if(condition2)
//……………………..
else if(condition3)
//……………………..
else
//…………………
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(“Failed n”);
printf(“You must take the course again! ”);
}
Two lines
Conditional Operator
? :
<expression-1>?<expression-2>:<expression-3>
printf(“%s”,(grade>=60)? “Passed”:“Failed”);
(grade>=60)?printf(“Passed”):printf(“Failed”);
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
int main() {
char c;
printf("Enter a Character ");
scanf("%c",&c);
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
Cellular Automata for Pathfinding
PPTX
Session04 selection structure_a
PPTX
Decision control
PPTX
Module 2- Control Structures
PPTX
Control Structures, If..else, switch..case.pptx
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
PPTX
control structure
PDF
Module 2 - Control Structures c programming.pptm.pdf
Cellular Automata for Pathfinding
Session04 selection structure_a
Decision control
Module 2- Control Structures
Control Structures, If..else, switch..case.pptx
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
control structure
Module 2 - Control Structures c programming.pptm.pdf

Similar to Session04 selection structure_b (20)

PPTX
Control Structures.pptx
PPTX
Selection statements on programming with C
PPTX
Ch05-converted.pptx
PDF
conditional_statement.pdf
PPTX
COM1407: Program Control Structures – Decision Making & Branching
PPTX
intro to CONTROL STRUCTURES 1 for C++.pptx
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
PDF
Ch05.pdf
PPT
Lec 04. If-Else Statement / Increment and Decrement Operators
PPT
3. control statements
PPTX
Control satkcher ppt
PPT
The Three Basic Selection Structures in C++ Programming Concepts
 
PPTX
Conditional statements
PDF
control statement
PPTX
unit 2-Control Structures.pptx
PPTX
Conditional Statements in C.pptx
PPT
Chapter 1 nested control structures
PPTX
Decision Control Structure If & Else
PPTX
Session05 iteration structure
PPTX
Structured Programming Unit-5-Control-Structure.pptx
Control Structures.pptx
Selection statements on programming with C
Ch05-converted.pptx
conditional_statement.pdf
COM1407: Program Control Structures – Decision Making & Branching
intro to CONTROL STRUCTURES 1 for C++.pptx
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Ch05.pdf
Lec 04. If-Else Statement / Increment and Decrement Operators
3. control statements
Control satkcher ppt
The Three Basic Selection Structures in C++ Programming Concepts
 
Conditional statements
control statement
unit 2-Control Structures.pptx
Conditional Statements in C.pptx
Chapter 1 nested control structures
Decision Control Structure If & Else
Session05 iteration structure
Structured Programming Unit-5-Control-Structure.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
DOC
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
Software Development Methodologies in 2025
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
PDF
Doc9.....................................
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
PDF
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
 
PDF
Transforming Manufacturing operations through Intelligent Integrations
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
creating-agentic-ai-solutions-leveraging-aws.pdf
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PDF
This slide provides an overview Technology
PDF
agentic-ai-and-the-future-of-autonomous-systems.pdf
Software Development Methodologies in 2025
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Doc9.....................................
GamePlan Trading System Review: Professional Trader's Honest Take
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Chapter 2 Digital Image Fundamentals.pdf
Understanding_Digital_Forensics_Presentation.pptx
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
CIFDAQ's Token Spotlight: SKY - A Forgotten Giant's Comeback?
 
Transforming Manufacturing operations through Intelligent Integrations
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
creating-agentic-ai-solutions-leveraging-aws.pdf
Automating ArcGIS Content Discovery with FME: A Real World Use Case
NewMind AI Weekly Chronicles - July'25 - Week IV
Smarter Business Operations Powered by IoT Remote Monitoring
This slide provides an overview Technology
agentic-ai-and-the-future-of-autonomous-systems.pdf

Session04 selection structure_b