Control Structures


Published on

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Control Structures

  1. 1. Control Structures A Lecture By Abdul Ghaffar Khan
  2. 2. Control Structure <ul><li>A computer program is generally composed of three types of structures generally called control structures. These control structures are </li></ul><ul><ul><ul><li>Sequential logic </li></ul></ul></ul><ul><ul><ul><li>Selection logic </li></ul></ul></ul><ul><ul><ul><li>Iteration logic </li></ul></ul></ul>
  3. 3. Sequential logic: <ul><li>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. </li></ul>
  4. 4. Selection logic <ul><li>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. </li></ul>
  5. 5. Iteration logic: <ul><li>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. </li></ul>
  6. 6. The if statement: <ul><li>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 </li></ul>
  7. 7. The if statement: <ul><li>The s yntax of the if statement is .. </li></ul><ul><li>if (boolean_expression) statement_set ; </li></ul><ul><li>or if (boolean_expression) //compound statements </li></ul><ul><li>{ </li></ul><ul><li>statement_set ; </li></ul><ul><li>…………… . </li></ul><ul><li>} </li></ul><ul><li>Where boolean_expression is an expression that evaluates 1 (true) or 0 (false). boolean_expression uses relational and logical operators </li></ul>
  8. 8. The if statement: ( Example 7.1) <ul><ul><li>#include<stdio.h> </li></ul></ul><ul><ul><li>void main(void) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><ul><li>int nbr; </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Enter a number? &quot;); </li></ul></ul></ul><ul><ul><ul><li>scanf(&quot;%d&quot;,&nbr); </li></ul></ul></ul><ul><ul><ul><li>if (nbr < 0) </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;%d is a negative number &quot;,nbr); </li></ul></ul></ul><ul><ul><ul><li>printf(&quot;Good bye &quot;);getche(); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Output: </li></ul><ul><li>Enter a number? -54 </li></ul><ul><li>-54 is a negative number </li></ul><ul><li>Good bye </li></ul>
  9. 9. Example 7.2 <ul><li>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. </li></ul>
  10. 10. Example 7.2 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><ul><li>int qty, dis=0, rate=30; </li></ul></ul><ul><ul><li>float amount; </li></ul></ul><ul><ul><li>clrscr(); </li></ul></ul><ul><ul><li>printf(&quot;Enter Quantity in liters &quot;); </li></ul></ul><ul><ul><li>scanf(&quot;%d&quot;,&qty); </li></ul></ul><ul><ul><li>if ( qty > 15) </li></ul></ul><ul><ul><li>dis = 5; </li></ul></ul><ul><ul><li>amount = qty * rate - qty * (rate * dis ) / 100.0; </li></ul></ul><ul><ul><li>printf(&quot;Discount is Rs. %.2f &quot;, qty *rate * dis /100.0); </li></ul></ul><ul><ul><li>printf(&quot;Your bill amount is Rs. %.2f &quot;, amount); </li></ul></ul><ul><ul><li>getche(); </li></ul></ul><ul><li>} </li></ul>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. 11. The if-else statement: <ul><ul><li>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. </li></ul></ul>
  12. 12. The if-else statement: <ul><ul><li>The syntax of if-else statement is </li></ul></ul><ul><ul><li>if (boolean_expression) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> statement_setl; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>statement_set2; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  13. 13. The if-else statement: (Example 7.3 Quadratic Equation )
  14. 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 &quot;); printf(&quot;Enter some other values of a, band c &quot;); } else { x1 = ( -b - sqrt( d )) / (2 * a); x2 = ( -b + sqrt( d )) / (2 * a); printf(&quot;First Root is %.2f &quot;,x1); printf(&quot;Second Root is %.2f &quot;,x2); } getch(); }
  15. 15. The Nested if statement: <ul><li>If an if statement is completely embedded within another if statement then this structure is known as nested if . Syntax of nested if is </li></ul><ul><ul><li>if (boolean_expression1) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li> statement_set1; </li></ul></ul><ul><ul><ul><li> if (boolean_exp2) </li></ul></ul></ul><ul><ul><ul><li> { </li></ul></ul></ul><ul><ul><ul><li> statement_set2; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li> { </li></ul></ul></ul><ul><ul><ul><li> statement_set3; </li></ul></ul></ul><ul><ul><ul><li> } </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>else </li></ul></ul><ul><ul><li> { </li></ul></ul><ul><ul><li> statement_set4; </li></ul></ul><ul><ul><li> } </li></ul></ul>
  16. 16. The else-if Structure: <ul><li>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. </li></ul>if (boolean_expression I) { statement_set1 ; } else if (boolean_exp2) { statement set2; }
  17. 17. <ul><li>Example 7.4: </li></ul><ul><ul><li> 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. </li></ul></ul>
  18. 18. Example 7.4
  19. 19. The switch Statement <ul><li>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 </li></ul><ul><ul><ul><ul><li>switch (expression) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>{ </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case value1: </li></ul></ul></ul></ul><ul><ul><ul><ul><li> statement_setl; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>break; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>case value2: </li></ul></ul></ul></ul><ul><ul><ul><ul><li> statement--set2; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>break; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>default: statement_setn; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
  20. 20. Conditional Operator ?: <ul><li>Conditional operator can be used instead of if statement. this operator is called ternary operator because it uses three expressions. Its syntax is </li></ul><ul><li>expression_l ? expression_2 : expression_3 </li></ul><ul><li>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 (). </li></ul>
  21. 21. Example 7.6 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int x. </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter a number &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&x );. </li></ul><ul><li>(x<O) ? printf(&quot;No is negative&quot;) : printf(&quot;No is positive or zero&quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>
  22. 22. Example 7.7 <ul><li>#include <stdio.h> </li></ul><ul><li>#include <conio.h> </li></ul><ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int y; </li></ul><ul><li>clrscr(); </li></ul><ul><li>printf(&quot;Enter a number &quot;); </li></ul><ul><li>scanf(&quot;%d&quot;,&y); </li></ul><ul><li>if ( (y%2) == 0 ) </li></ul><ul><li>printf(&quot;given number is an even number &quot;); </li></ul><ul><li>else </li></ul><ul><li>printf(&quot;Given number is an odd number &quot;); </li></ul><ul><li>getch(); </li></ul><ul><li>} </li></ul>