SlideShare a Scribd company logo
Control Structures A Lecture By Abdul Ghaffar Khan
Control Structure A computer program is generally composed of three types of structures generally called control structures. These control structures are Sequential logic Selection logic Iteration logic
Sequential logic: This is the default logic used by every compiler. In this logic the program instructions are executed in the order in which they appeared in the program. The compiler scans the program, instruction by instruction and run the instruction one by one from top to bottom. This is also called linear logic.
Selection logic Also called conditional logic, used to execute a set of instructions depending over a condition. This logic is also known as branching. C-Language supports  if   and  switch  statements for selection logic. A conditional operator  ?:   is already discussed in the last chapter.
Iteration logic: If a group of instructions is executed repeatedly, until some logical condition has been satisfied. Then the logic is called Iteration logic, repetition logic or looping. C-Language provides  many looping function like  while ,  do-while ,  and   for   etc.
The if statement: The if statement is used to test a condition and then take a possible action, depending on the condition. This situation can be expressed using the following block diagram
The if statement: The s yntax of the if statement is .. if (boolean_expression)  statement_set ; or  if (boolean_expression)  //compound statements { statement_set ; โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ . } Where boolean_expression is an expression that evaluates 1 (true) or 0 (false). boolean_expression uses relational and logical operators
The if statement:  ( Example 7.1) #include<stdio.h> void main(void) { int nbr; printf(&quot;Enter a number? &quot;); scanf(&quot;%d&quot;,&nbr); if (nbr < 0)  printf(&quot;%d is a negative number \n&quot;,nbr); printf(&quot;Good bye \n&quot;);getche(); } Output: Enter a number? -54 -54 is a negative number Good bye
Example 7.2 A petrol pump offer 5% discount to his customers who purchase more than 15 liters petrol from this pump. Write down a program to calculate user bill amount if the quantity is entered through keyboard and the price per liter is Rs. 30.
Example 7.2 #include <stdio.h> #include <conio.h> void main(void) { int qty, dis=0, rate=30; float amount; clrscr(); printf(&quot;Enter Quantity in liters &quot;); scanf(&quot;%d&quot;,&qty); if ( qty > 15) dis = 5; amount = qty * rate - qty * (rate * dis ) / 100.0; printf(&quot;Discount is Rs. %.2f\n&quot;, qty *rate * dis /100.0); printf(&quot;Your bill amount is Rs. %.2f\n&quot;, amount); getche(); } Output: 1)  Enter Quantity in liters 25 : Discount is Rs 37.50  Your bill amount is Rs 712.50 2) Enter Quantity in liters 10 Discount is Rs. 0.00 Your bill amount is Rs.300.00
The  if-else   statement: The if-else is a structure which executes a set of instructions if  a condition  is true, otherwise if it is false it executes another set of statements. This situation can be represented by the following diagram.
The  if-else   statement: The syntax of if-else statement is  if (boolean_expression) {   statement_setl; } else { statement_set2; }
The  if-else   statement:  (Example 7.3 Quadratic Equation )
The  if-else   statement:  (Example 7.3 Quadratic Equation ) #include <stdio.h> #include <conio.h>  #include <math.h> void main(void) { int a, b, c; double d, x1, x2; //clrscr(); printf(&quot;Input a=&quot;); scanf(&quot;%d&quot;,&a); printf(&quot;input b = &quot;); scanf(&quot;%d&quot;,&b ); printf(&quot;input c = &quot;); scanf(&quot;%d&quot;,&c ); d = b*b -4*a*c; if (d<0)  {  printf(&quot;Roots are not real\n&quot;); printf(&quot;Enter some other values of a, band c\n&quot;); } else { x1 = ( -b - sqrt( d )) / (2 * a); x2 = ( -b + sqrt( d )) / (2 * a); printf(&quot;First Root is %.2f\n&quot;,x1); printf(&quot;Second Root is %.2f\n&quot;,x2); }  getch(); }
The Nested  if   statement: If an  if  statement is completely embedded within another  if  statement then this structure is known as  nested if . Syntax of nested if is if (boolean_expression1) {   statement_set1;   if  (boolean_exp2)   {   statement_set2; } else   {   statement_set3;   } } else   {   statement_set4;   }
The  else-if   Structure: another example of nested if statement is to put a complete if statement within the else part of another if statement. The syntax of such structure is. if (boolean_expression I) { statement_set1 ; } else if (boolean_exp2)   {   statement set2;   }
Example 7.4:   This sample C program calculates the area of a Triangle, Rectangle, or a circle depending on the user input. Program first asks the shape character. Through keyboard then checks the appropriate character for the shape name and calculate the area of that shape.
Example 7.4
The switch Statement The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based on the value of expression, which is included in the switch statement. The general syntax of switch statement is  switch (expression) { case value1:   statement_setl;  break; case value2:   statement--set2;  break; default: statement_setn; }
Conditional Operator ?: Conditional  operator can be used instead of if statement. this operator is called ternary operator because it uses three expressions. Its syntax is  expression_l ? expression_2 : expression_3 expression_1 is a logical expression which is evaluated first, if it is true then expression_2 is evaluated and its value becomes the value of whole conditional expression otherwise expression_3 is evaluated and its value becomes the value of whole conditional expression. It means out of two expressions either expression_2 or expression_1 is evaluated. Because expression_1 is a logical expression it must be enclosed in a pair of brackets ().
Example 7.6 #include <stdio.h> #include <conio.h> void main(void) { int x. clrscr(); printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;,&x );. (x<O) ? printf(&quot;No is negative&quot;) : printf(&quot;No is positive or zero&quot;); getch(); }
Example 7.7 #include <stdio.h>  #include <conio.h>  void main(void) { int y; clrscr(); printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;,&y); if ( (y%2) == 0 ) printf(&quot;given number is an even number\n &quot;); else printf(&quot;Given number is an odd number\n&quot;); getch(); }
Ad

More Related Content

What's hot (20)

C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
ย 
C if else
C if elseC if else
C if else
Ritwik Das
ย 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
ย 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
ย 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
ย 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
ย 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
ย 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
ย 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
amber chaudary
ย 
10. switch case
10. switch case10. switch case
10. switch case
Way2itech
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
ย 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
ย 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
ย 
Loops in c++ programming language
Loops in c++ programming language Loops in c++ programming language
Loops in c++ programming language
MUHAMMAD ALI student of IT at karakoram International university gilgit baltistan
ย 
structure and union
structure and unionstructure and union
structure and union
student
ย 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
ย 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
ย 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
ย 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
Anjan Mahanta
ย 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
ย 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
ย 
C if else
C if elseC if else
C if else
Ritwik Das
ย 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
ย 
Control Flow Statements
Control Flow Statements Control Flow Statements
Control Flow Statements
Tarun Sharma
ย 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
ย 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
ย 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
ย 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
ย 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
amber chaudary
ย 
10. switch case
10. switch case10. switch case
10. switch case
Way2itech
ย 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
ย 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
ย 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
ย 
structure and union
structure and unionstructure and union
structure and union
student
ย 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
Samiksha Pun
ย 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
ย 
Visual Basic 6.0
Visual Basic 6.0Visual Basic 6.0
Visual Basic 6.0
Anjan Mahanta
ย 
Operators in C++
Operators in C++Operators in C++
Operators in C++
Sachin Sharma
ย 

Similar to Control Structures (20)

C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
ย 
C language UPTU Unit3 Slides
C language UPTU Unit3 SlidesC language UPTU Unit3 Slides
C language UPTU Unit3 Slides
Anurag University Hyderabad
ย 
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
ย 
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdfPrincipals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
anilcsbs
ย 
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
ย 
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
ย 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
ย 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
ย 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
ย 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
ย 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
SENA
ย 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
ย 
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
PPS 3.3CONDITIONAL BRANCHING AND LOOPS  WRITING AND EVALUATION OF CONDITIONAL...PPS 3.3CONDITIONAL BRANCHING AND LOOPS  WRITING AND EVALUATION OF CONDITIONAL...
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
ย 
Ch04
Ch04Ch04
Ch04
Arriz San Juan
ย 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Rajeshkumar Reddy
ย 
7 decision-control
7 decision-control7 decision-control
7 decision-control
Rohit Shrivastava
ย 
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
ย 
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
ย 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
Praveen M Jigajinni
ย 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
Indraprastha Institute of Information Technology
ย 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
Vikram Nandini
ย 
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
ย 
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdfPrincipals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
anilcsbs
ย 
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
ย 
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
ย 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
nmahi96
ย 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
ย 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
ย 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
ย 
C programming decision making
C programming decision makingC programming decision making
C programming decision making
SENA
ย 
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow controlIntroduction to computer programming (C)-CSC1205_Lec5_Flow control
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
ENGWAU TONNY
ย 
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
PPS 3.3CONDITIONAL BRANCHING AND LOOPS  WRITING AND EVALUATION OF CONDITIONAL...PPS 3.3CONDITIONAL BRANCHING AND LOOPS  WRITING AND EVALUATION OF CONDITIONAL...
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
ย 
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDYC UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
Rajeshkumar Reddy
ย 
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
ย 
Ad

More from Ghaffar Khan (20)

World is beautiful ... ...
World is beautiful ... ...World is beautiful ... ...
World is beautiful ... ...
Ghaffar Khan
ย 
My Presentation On Ajax
My Presentation On AjaxMy Presentation On Ajax
My Presentation On Ajax
Ghaffar Khan
ย 
Sorting
SortingSorting
Sorting
Ghaffar Khan
ย 
How A Computer Works
How A Computer WorksHow A Computer Works
How A Computer Works
Ghaffar Khan
ย 
For Loop
For LoopFor Loop
For Loop
Ghaffar Khan
ย 
Exponential and Logarthmic funtions
Exponential and Logarthmic funtionsExponential and Logarthmic funtions
Exponential and Logarthmic funtions
Ghaffar Khan
ย 
Exponential and Logarthmic funtions (1)
Exponential and Logarthmic funtions (1)Exponential and Logarthmic funtions (1)
Exponential and Logarthmic funtions (1)
Ghaffar Khan
ย 
Functions
FunctionsFunctions
Functions
Ghaffar Khan
ย 
Quadratic And Polinomial Function
Quadratic And Polinomial FunctionQuadratic And Polinomial Function
Quadratic And Polinomial Function
Ghaffar Khan
ย 
Quadratic And Polinomial Function
 Quadratic And Polinomial Function Quadratic And Polinomial Function
Quadratic And Polinomial Function
Ghaffar Khan
ย 
Exponentioal And Logarthmic Functions
 Exponentioal And Logarthmic Functions Exponentioal And Logarthmic Functions
Exponentioal And Logarthmic Functions
Ghaffar Khan
ย 
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
Ghaffar Khan
ย 
Introduction to Computer Networks
 Introduction to Computer Networks Introduction to Computer Networks
Introduction to Computer Networks
Ghaffar Khan
ย 
Network Layer
Network LayerNetwork Layer
Network Layer
Ghaffar Khan
ย 
Input And Output
 Input And Output Input And Output
Input And Output
Ghaffar Khan
ย 
Surfaces
SurfacesSurfaces
Surfaces
Ghaffar Khan
ย 
Vector Tools
Vector ToolsVector Tools
Vector Tools
Ghaffar Khan
ย 
Drawing Tools
Drawing ToolsDrawing Tools
Drawing Tools
Ghaffar Khan
ย 
Drawing Figures
Drawing FiguresDrawing Figures
Drawing Figures
Ghaffar Khan
ย 
Computer Graphics Introduction
Computer Graphics IntroductionComputer Graphics Introduction
Computer Graphics Introduction
Ghaffar Khan
ย 
World is beautiful ... ...
World is beautiful ... ...World is beautiful ... ...
World is beautiful ... ...
Ghaffar Khan
ย 
My Presentation On Ajax
My Presentation On AjaxMy Presentation On Ajax
My Presentation On Ajax
Ghaffar Khan
ย 
How A Computer Works
How A Computer WorksHow A Computer Works
How A Computer Works
Ghaffar Khan
ย 
Exponential and Logarthmic funtions
Exponential and Logarthmic funtionsExponential and Logarthmic funtions
Exponential and Logarthmic funtions
Ghaffar Khan
ย 
Exponential and Logarthmic funtions (1)
Exponential and Logarthmic funtions (1)Exponential and Logarthmic funtions (1)
Exponential and Logarthmic funtions (1)
Ghaffar Khan
ย 
Functions
FunctionsFunctions
Functions
Ghaffar Khan
ย 
Quadratic And Polinomial Function
Quadratic And Polinomial FunctionQuadratic And Polinomial Function
Quadratic And Polinomial Function
Ghaffar Khan
ย 
Quadratic And Polinomial Function
 Quadratic And Polinomial Function Quadratic And Polinomial Function
Quadratic And Polinomial Function
Ghaffar Khan
ย 
Exponentioal And Logarthmic Functions
 Exponentioal And Logarthmic Functions Exponentioal And Logarthmic Functions
Exponentioal And Logarthmic Functions
Ghaffar Khan
ย 
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
Ghaffar Khan
ย 
Introduction to Computer Networks
 Introduction to Computer Networks Introduction to Computer Networks
Introduction to Computer Networks
Ghaffar Khan
ย 
Network Layer
Network LayerNetwork Layer
Network Layer
Ghaffar Khan
ย 
Input And Output
 Input And Output Input And Output
Input And Output
Ghaffar Khan
ย 
Vector Tools
Vector ToolsVector Tools
Vector Tools
Ghaffar Khan
ย 
Drawing Tools
Drawing ToolsDrawing Tools
Drawing Tools
Ghaffar Khan
ย 
Drawing Figures
Drawing FiguresDrawing Figures
Drawing Figures
Ghaffar Khan
ย 
Computer Graphics Introduction
Computer Graphics IntroductionComputer Graphics Introduction
Computer Graphics Introduction
Ghaffar Khan
ย 
Ad

Recently uploaded (20)

TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
ย 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
ย 
Drupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Drupalcamp Finland โ€“ Measuring Front-end Energy ConsumptionDrupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Drupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Exove
ย 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
ย 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
ย 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
ย 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
ย 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
ย 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
ย 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
ย 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
ย 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
ย 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
ย 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
ย 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
ย 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
ย 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
ย 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
ย 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
ย 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
ย 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
ย 
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
IEDM 2024 Tutorial2_Advances in CMOS Technologies and Future Directions for C...
organizerofv
ย 
Drupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Drupalcamp Finland โ€“ Measuring Front-end Energy ConsumptionDrupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Drupalcamp Finland โ€“ Measuring Front-end Energy Consumption
Exove
ย 
Linux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdfLinux Professional Institute LPIC-1 Exam.pdf
Linux Professional Institute LPIC-1 Exam.pdf
RHCSA Guru
ย 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
ย 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
ย 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
ย 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
ย 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
ย 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
ย 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
ย 
How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?How Can I use the AI Hype in my Business Context?
How Can I use the AI Hype in my Business Context?
Daniel Lehner
ย 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
ย 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
ย 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
ย 
Technology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data AnalyticsTechnology Trends in 2025: AI and Big Data Analytics
Technology Trends in 2025: AI and Big Data Analytics
InData Labs
ย 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
ย 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
ย 
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul Shares 5 Steps to Implement AI Agents for Maximum Business Efficien...
Noah Loul
ย 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
ย 

Control Structures

  • 1. Control Structures A Lecture By Abdul Ghaffar Khan
  • 2. Control Structure A computer program is generally composed of three types of structures generally called control structures. These control structures are Sequential logic Selection logic Iteration logic
  • 3. Sequential logic: This is the default logic used by every compiler. In this logic the program instructions are executed in the order in which they appeared in the program. The compiler scans the program, instruction by instruction and run the instruction one by one from top to bottom. This is also called linear logic.
  • 4. Selection logic Also called conditional logic, used to execute a set of instructions depending over a condition. This logic is also known as branching. C-Language supports if and switch statements for selection logic. A conditional operator ?: is already discussed in the last chapter.
  • 5. Iteration logic: If a group of instructions is executed repeatedly, until some logical condition has been satisfied. Then the logic is called Iteration logic, repetition logic or looping. C-Language provides many looping function like while , do-while , and for etc.
  • 6. The if statement: The if statement is used to test a condition and then take a possible action, depending on the condition. This situation can be expressed using the following block diagram
  • 7. The if statement: The s yntax of the if statement is .. if (boolean_expression) statement_set ; or if (boolean_expression) //compound statements { statement_set ; โ€ฆโ€ฆโ€ฆโ€ฆโ€ฆ . } Where boolean_expression is an expression that evaluates 1 (true) or 0 (false). boolean_expression uses relational and logical operators
  • 8. The if statement: ( Example 7.1) #include<stdio.h> void main(void) { int nbr; printf(&quot;Enter a number? &quot;); scanf(&quot;%d&quot;,&nbr); if (nbr < 0) printf(&quot;%d is a negative number \n&quot;,nbr); printf(&quot;Good bye \n&quot;);getche(); } Output: Enter a number? -54 -54 is a negative number Good bye
  • 9. Example 7.2 A petrol pump offer 5% discount to his customers who purchase more than 15 liters petrol from this pump. Write down a program to calculate user bill amount if the quantity is entered through keyboard and the price per liter is Rs. 30.
  • 10. Example 7.2 #include <stdio.h> #include <conio.h> void main(void) { int qty, dis=0, rate=30; float amount; clrscr(); printf(&quot;Enter Quantity in liters &quot;); scanf(&quot;%d&quot;,&qty); if ( qty > 15) dis = 5; amount = qty * rate - qty * (rate * dis ) / 100.0; printf(&quot;Discount is Rs. %.2f\n&quot;, qty *rate * dis /100.0); printf(&quot;Your bill amount is Rs. %.2f\n&quot;, amount); getche(); } Output: 1) Enter Quantity in liters 25 : Discount is Rs 37.50 Your bill amount is Rs 712.50 2) Enter Quantity in liters 10 Discount is Rs. 0.00 Your bill amount is Rs.300.00
  • 11. The if-else statement: The if-else is a structure which executes a set of instructions if a condition is true, otherwise if it is false it executes another set of statements. This situation can be represented by the following diagram.
  • 12. The if-else statement: The syntax of if-else statement is if (boolean_expression) { statement_setl; } else { statement_set2; }
  • 13. The if-else statement: (Example 7.3 Quadratic Equation )
  • 14. The if-else statement: (Example 7.3 Quadratic Equation ) #include <stdio.h> #include <conio.h> #include <math.h> void main(void) { int a, b, c; double d, x1, x2; //clrscr(); printf(&quot;Input a=&quot;); scanf(&quot;%d&quot;,&a); printf(&quot;input b = &quot;); scanf(&quot;%d&quot;,&b ); printf(&quot;input c = &quot;); scanf(&quot;%d&quot;,&c ); d = b*b -4*a*c; if (d<0) { printf(&quot;Roots are not real\n&quot;); printf(&quot;Enter some other values of a, band c\n&quot;); } else { x1 = ( -b - sqrt( d )) / (2 * a); x2 = ( -b + sqrt( d )) / (2 * a); printf(&quot;First Root is %.2f\n&quot;,x1); printf(&quot;Second Root is %.2f\n&quot;,x2); } getch(); }
  • 15. The Nested if statement: If an if statement is completely embedded within another if statement then this structure is known as nested if . Syntax of nested if is if (boolean_expression1) { statement_set1; if (boolean_exp2) { statement_set2; } else { statement_set3; } } else { statement_set4; }
  • 16. The else-if Structure: another example of nested if statement is to put a complete if statement within the else part of another if statement. The syntax of such structure is. if (boolean_expression I) { statement_set1 ; } else if (boolean_exp2) { statement set2; }
  • 17. Example 7.4: This sample C program calculates the area of a Triangle, Rectangle, or a circle depending on the user input. Program first asks the shape character. Through keyboard then checks the appropriate character for the shape name and calculate the area of that shape.
  • 19. The switch Statement The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based on the value of expression, which is included in the switch statement. The general syntax of switch statement is switch (expression) { case value1: statement_setl; break; case value2: statement--set2; break; default: statement_setn; }
  • 20. Conditional Operator ?: Conditional operator can be used instead of if statement. this operator is called ternary operator because it uses three expressions. Its syntax is expression_l ? expression_2 : expression_3 expression_1 is a logical expression which is evaluated first, if it is true then expression_2 is evaluated and its value becomes the value of whole conditional expression otherwise expression_3 is evaluated and its value becomes the value of whole conditional expression. It means out of two expressions either expression_2 or expression_1 is evaluated. Because expression_1 is a logical expression it must be enclosed in a pair of brackets ().
  • 21. Example 7.6 #include <stdio.h> #include <conio.h> void main(void) { int x. clrscr(); printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;,&x );. (x<O) ? printf(&quot;No is negative&quot;) : printf(&quot;No is positive or zero&quot;); getch(); }
  • 22. Example 7.7 #include <stdio.h> #include <conio.h> void main(void) { int y; clrscr(); printf(&quot;Enter a number &quot;); scanf(&quot;%d&quot;,&y); if ( (y%2) == 0 ) printf(&quot;given number is an even number\n &quot;); else printf(&quot;Given number is an odd number\n&quot;); getch(); }