SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
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(); }

More Related Content

What's hot (20)

PPTX
Preprocessor directives in c language
tanmaymodi4
 
PPT
Control structure C++
Anil Kumar
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
Exception handling c++
Jayant Dalvi
 
PDF
Loops and conditional statements
Saad Sheikh
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PDF
10. switch case
Way2itech
 
PDF
Algorithm and c language
kamalbeydoun
 
PPTX
Applets in java
Wani Zahoor
 
PPT
RECURSION IN C
v_jk
 
PPSX
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
PPTX
Character Arrays and strings in c language
mallikavin
 
PPTX
Assembly Language
Ibrahimcommunication Al Ani
 
PDF
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
DOCX
Basic structure of c programming
TejaswiB4
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPT
structure and union
student
 
PPTX
Data types in C
Tarun Sharma
 
Preprocessor directives in c language
tanmaymodi4
 
Control structure C++
Anil Kumar
 
C Programming: Control Structure
Sokngim Sa
 
Exception handling c++
Jayant Dalvi
 
Loops and conditional statements
Saad Sheikh
 
Function C programming
Appili Vamsi Krishna
 
Exception Handling in object oriented programming using C++
Janki Shah
 
10. switch case
Way2itech
 
Algorithm and c language
kamalbeydoun
 
Applets in java
Wani Zahoor
 
RECURSION IN C
v_jk
 
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Character Arrays and strings in c language
mallikavin
 
Assembly Language
Ibrahimcommunication Al Ani
 
Introduction to c++ ppt 1
Prof. Dr. K. Adisesha
 
Basic structure of c programming
TejaswiB4
 
classes and objects in C++
HalaiHansaika
 
structure and union
student
 
Data types in C
Tarun Sharma
 

Similar to Control Structures (20)

PPT
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
PPT
Ch3 selection
Hattori Sidek
 
PPTX
C Unit-2.ppt
Giri383500
 
PPTX
CONTROL FLOW in C.pptx
SmitaAparadh
 
PPTX
computer programming and utilization
JAYDEV PATEL
 
PPTX
unit 2-Control Structures.pptx
ishaparte4
 
PPTX
Control Structures.pptx
ssuserfb3c3e
 
PPTX
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
PDF
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
PDF
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
DOCX
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
 
PPTX
Decision Making and Branching
Munazza-Mah-Jabeen
 
PPTX
Module 2- Control Structures
nikshaikh786
 
PPSX
Bsit1
jigeno
 
PPTX
Structured Programming Unit-5-Control-Structure.pptx
SuryaBasnet1
 
PDF
control statement
Kathmandu University
 
PDF
chapter-4 slide.pdf
cricketreview
 
PPTX
Control structure of c
Komal Kotak
 
PPTX
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
C language UPTU Unit3 Slides
Anurag University Hyderabad
 
Ch3 selection
Hattori Sidek
 
C Unit-2.ppt
Giri383500
 
CONTROL FLOW in C.pptx
SmitaAparadh
 
computer programming and utilization
JAYDEV PATEL
 
unit 2-Control Structures.pptx
ishaparte4
 
Control Structures.pptx
ssuserfb3c3e
 
control_structures_c_language_regarding how to represent the loop in language...
ShirishaBuduputi
 
Module 2 - Control Structures c programming.pptm.pdf
SudipDebnath20
 
Unit ii chapter 2 Decision making and Branching in C
Sowmya Jyothi
 
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
Sitamarhi Institute of Technology
 
Decision Making and Branching
Munazza-Mah-Jabeen
 
Module 2- Control Structures
nikshaikh786
 
Bsit1
jigeno
 
Structured Programming Unit-5-Control-Structure.pptx
SuryaBasnet1
 
control statement
Kathmandu University
 
chapter-4 slide.pdf
cricketreview
 
Control structure of c
Komal Kotak
 
6 Control Structures-1.pptxAAAAAAAAAAAAAAAAAAAAA
EG20910848921ISAACDU
 
Ad

More from Ghaffar Khan (20)

PPT
World is beautiful ... ...
Ghaffar Khan
 
PPTX
My Presentation On Ajax
Ghaffar Khan
 
PPT
Sorting
Ghaffar Khan
 
PPT
How A Computer Works
Ghaffar Khan
 
PPT
For Loop
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions
Ghaffar Khan
 
PPT
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
PPT
Functions
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Quadratic And Polinomial Function
Ghaffar Khan
 
PPT
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
PPT
Internet Protocol
Ghaffar Khan
 
PPT
Introduction to Computer Networks
Ghaffar Khan
 
PPT
Network Layer
Ghaffar Khan
 
PPT
Input And Output
Ghaffar Khan
 
PPT
Surfaces
Ghaffar Khan
 
PPT
Vector Tools
Ghaffar Khan
 
PPT
Drawing Tools
Ghaffar Khan
 
PPT
Drawing Figures
Ghaffar Khan
 
PPT
Computer Graphics Introduction
Ghaffar Khan
 
World is beautiful ... ...
Ghaffar Khan
 
My Presentation On Ajax
Ghaffar Khan
 
Sorting
Ghaffar Khan
 
How A Computer Works
Ghaffar Khan
 
For Loop
Ghaffar Khan
 
Exponential and Logarthmic funtions
Ghaffar Khan
 
Exponential and Logarthmic funtions (1)
Ghaffar Khan
 
Functions
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Quadratic And Polinomial Function
Ghaffar Khan
 
Exponentioal And Logarthmic Functions
Ghaffar Khan
 
Internet Protocol
Ghaffar Khan
 
Introduction to Computer Networks
Ghaffar Khan
 
Network Layer
Ghaffar Khan
 
Input And Output
Ghaffar Khan
 
Surfaces
Ghaffar Khan
 
Vector Tools
Ghaffar Khan
 
Drawing Tools
Ghaffar Khan
 
Drawing Figures
Ghaffar Khan
 
Computer Graphics Introduction
Ghaffar Khan
 
Ad

Recently uploaded (20)

PPTX
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Complete Network Protection with Real-Time Security
L4RGINDIA
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Building a Production-Ready Barts Health Secure Data Environment Tooling, Acc...
Barts Health
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Complete Network Protection with Real-Time Security
L4RGINDIA
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 

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(); }