• Save
Control Structures
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Control Structures

on

  • 4,188 views

 

Statistics

Views

Total Views
4,188
Views on SlideShare
4,123
Embed Views
65

Actions

Likes
5
Downloads
0
Comments
1

13 Embeds 65

http://ghaffarkh.blogspot.com 37
http://ghaffarkh.blogspot.in 15
http://ghaffarkh.blogspot.de 2
http://ghaffarkh.blogspot.co.uk 2
http://ghaffarkh.blogspot.com.au 1
http://ghaffarkh.blogspot.ru 1
http://ghaffarkh.blogspot.ro 1
http://www.slideshare.net 1
http://ghaffarkh.blogspot.pt 1
http://ghaffarkh.blogspot.be 1
http://ghaffarkh.blogspot.com.es 1
http://ghaffarkh.blogspot.co.nz 1
http://ghaffarkh.blogspot.fr 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • nice
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Control Structures Presentation Transcript

  • 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 &quot;,nbr);
        • printf(&quot;Good bye &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 &quot;, qty *rate * dis /100.0);
      • printf(&quot;Your bill amount is Rs. %.2f &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 &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. 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. Example 7.4
  • 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 &quot;);
    • else
    • printf(&quot;Given number is an odd number &quot;);
    • getch();
    • }