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

Control Structures

  • 1.
    Control Structures ALecture By Abdul Ghaffar Khan
  • 2.
    Control Structure Acomputer 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: Thisis 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 Alsocalled 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: Ifa 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 Apetrol 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.
  • 18.
  • 19.
    The switch StatementThe 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(); }