2
Most read
3
Most read
22
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

PPT
multimedia element
PPTX
Getting Started with Azure AI Studio.pptx
PPTX
Line follower robot
PPTX
Speech recognition final presentation
PPT
Orthographic Projection
PPTX
Arduino Functions
PPTX
Rfid ppt 8th sem
PPTX
PRINCIPLES OF CURRICULUM
multimedia element
Getting Started with Azure AI Studio.pptx
Line follower robot
Speech recognition final presentation
Orthographic Projection
Arduino Functions
Rfid ppt 8th sem
PRINCIPLES OF CURRICULUM

What's hot (20)

PPT
Control structure C++
PPTX
Introduction to Selection control structures in C++
PPSX
Type conversion
PPTX
Functions in c++
PPTX
Compiler vs interpreter
PPT
Function overloading(c++)
PPT
FUNCTIONS IN c++ PPT
PPTX
Templates in C++
PPTX
Introduction to programming
PPTX
Operators and expressions in c language
PPTX
Loops c++
PPT
Input and output in C++
PPTX
Member Function in C++
PPT
PPTX
Static Members-Java.pptx
PPTX
Basic Data Types in C++
PPTX
Conditional and control statement
PPTX
Control structures in c++
PPTX
Function in C program
Control structure C++
Introduction to Selection control structures in C++
Type conversion
Functions in c++
Compiler vs interpreter
Function overloading(c++)
FUNCTIONS IN c++ PPT
Templates in C++
Introduction to programming
Operators and expressions in c language
Loops c++
Input and output in C++
Member Function in C++
Static Members-Java.pptx
Basic Data Types in C++
Conditional and control statement
Control structures in c++
Function in C program
Ad

Similar to Control Structures (20)

PPTX
C Programming Unit-2
PPT
C language UPTU Unit3 Slides
PPTX
Basics of Control Statement in C Languages
PDF
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
PPT
Decision making in C(2020-2021) statements
PPT
C statements.ppt presentation in c language
PDF
Control statements-Computer programming
PPS
Programming in Arduino (Part 2)
PPTX
Dti2143 chap 4 control structures aka_selection
PPTX
Dti2143 chap 4 control structures aka_selection
PDF
C programming decision making
PDF
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
DOCX
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
DOCX
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
PPTX
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
PPT
7 decision-control
PPT
Chapter 4 flow control structures and arrays
PPT
Control statments in c
PPT
Flow of Control
C Programming Unit-2
C language UPTU Unit3 Slides
Basics of Control Statement in C Languages
Principals of Programming in CModule -5.pdfModule_2_P2 (1).pdf
Decision making in C(2020-2021) statements
C statements.ppt presentation in c language
Control statements-Computer programming
Programming in Arduino (Part 2)
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
C programming decision making
Introduction to computer programming (C)-CSC1205_Lec5_Flow control
PPS 3.3CONDITIONAL BRANCHING AND LOOPS WRITING AND EVALUATION OF CONDITIONAL...
C UNIT-2 PREPARED Y M V BRAHMANANDA REDDY
C PROGRAMMING-CONTROL STATEMENT (IF-ELSE, SWITCH)
7 decision-control
Chapter 4 flow control structures and arrays
Control statments in c
Flow of Control
Ad

More from Ghaffar Khan (20)

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

Recently uploaded (20)

PDF
Human Computer Interaction Miterm Lesson
PDF
Streamline Vulnerability Management From Minimal Images to SBOMs
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
Lung cancer patients survival prediction using outlier detection and optimize...
PPTX
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
Examining Bias in AI Generated News Content.pdf
PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
The AI Revolution in Customer Service - 2025
PDF
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
PDF
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
PPTX
Presentation - Principles of Instructional Design.pptx
PDF
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
PDF
Decision Optimization - From Theory to Practice
PPTX
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
PDF
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
PDF
giants, standing on the shoulders of - by Daniel Stenberg
Human Computer Interaction Miterm Lesson
Streamline Vulnerability Management From Minimal Images to SBOMs
Connector Corner: Transform Unstructured Documents with Agentic Automation
Lung cancer patients survival prediction using outlier detection and optimize...
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
Examining Bias in AI Generated News Content.pdf
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
Data Virtualization in Action: Scaling APIs and Apps with FME
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
The AI Revolution in Customer Service - 2025
ment.tech-Siri Delay Opens AI Startup Opportunity in 2025.pdf
Transform-Your-Factory-with-AI-Driven-Quality-Engineering.pdf
Presentation - Principles of Instructional Design.pptx
5-Ways-AI-is-Revolutionizing-Telecom-Quality-Engineering.pdf
Decision Optimization - From Theory to Practice
AQUEEL MUSHTAQUE FAKIH COMPUTER CENTER .
Transform-Your-Streaming-Platform-with-AI-Driven-Quality-Engineering.pdf
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
giants, standing on the shoulders of - by Daniel Stenberg

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