Docstoc

05. If statements

Document Sample
05.  If statements Powered By Docstoc
					         CGS 3460




Conditional Execution
                                     CGS 3460


                                If statement
   So far, we have had sequential execution (everything is
    executed)
   Offers little flexibility
   May want code executed only when certain condition is
    met
      Conditional Execution
         • Execute a statement or statements only if a certain condition is met
         • Or not met
                                         CGS 3460


                                   If statement
   Consists of keyword if
      Followed by condition in parenthesis
      Followed by the body of the if statement
         • This is what is executed if the condition evaluates to true
         • Body can consist of multiple statements if they are enclosed with { }


    Operator                       Meaning                              Example

       ==                        Equal to                              var == 10
       !=                      Not equal to                            var != 10
        <                       Less than                               var < 10
       <=                  Less than or equal to                       var <= 10
        >                      Greater than                             var > 10
       >=                 Greater than or equal to                     var >= 10
                                     CGS 3460


                                 The if Statement
   Format
     if ( condition )
         program statement;
    or                                      Condition    No
     if ( condition )                       satisfied?
     {
         program statement(s);
     }                                            Yes

                                            Program
                                            statement
                                        CGS 3460


                                       Example
   Calculating the absolute value of an integer



                                                                           No
                                                         number < 0
    printf(“type in your number: “);
    scanf(“%i”, &number);
                                                               Yes
    if ( number < 0 )
          number = -number;                           number = - number;

    printf(“\nThe absolute value is %i\n”, number);
                                  CGS 3460


                                   Notes
   Only multiply by -1 if the number is less than 0
   The if statement doesn’t end in semi-colon
      The body does, but the if statement doesn’t
      The if statement begins and ends with brace ( {…} )
      The body has one or more statements within the braces ( {…} )
         • Can be considered a compound statement since the body can have
           multiple statements
                                     CGS 3460


                                    If - else
   Sometimes have two sets of statements and only want to
    execute one based on a conditional test
   Definitely want to execute one or the other, but not both
   If the body of the if statement is skipped (due to the
    condition evaluating to false) the body of the else
    statement is executed
   Consists of key word if
      Followed by condition in parenthesis
      Followed by the body of the if
   Followed by key word else
      Followed by condition in parenthesis
      Followed by the body of the else
                                   CGS 3460


                          The if-else Statement
   Format
    if ( condition )
        program statement 1;
     else                               Condition     No
        program statement 2;            satisfied?
   Or
     if ( condition )                         Yes
     {
         program statement(s) 1;        Program        Program
     }                                  statement 1    statement 2
     else
     {
         program statement(s) 2;
     }
                                       CGS 3460


            The if-else Statement - Example
Are you a gator?

                                                             No
                                            answer == ‘y’


 printf(“Are you a gator? (y)es, (n)o “);
                                                    Yes
 scanf(“%c”, &answer);
                                              “Go Gators!”    “Gator Bait"
 if ( answer == ‘y’)
       printf(“Go Gators!!!\n”);
 else
       printf(“Then you’re gator bait”);
                                            CGS 3460

                                           Example
                                            Are you a gator? (y)es, (n)o
printf(“Are you a gator? (y)es, (n)o “);    y
scanf(“%c”, &answer);                       Go Gators!!!

if ( answer == ‘y’)
       printf(“Go Gators!!!\n”);
else                                         Are you a gator? (y)es, (n)o
       printf(“Then you’re gator bait”);     n
                                             Then you’re gator bait!




                                             Are you a gator? (y)es, (n)o
                                             Y
                                             Then you’re gator bait!
                                        CGS 3460

                                        else if
   Seen two statements to facilitate conditional execution
      if statement
           • selectively executes a set of statments based on a conditional test.
      if..else
           • allows us to make one of two choices
           • if the condition is true, the statements in the body of if is executed,
           • otherwise the statements in the body of the else part are
             automatically executed.
   Sometimes need to choose between multiple alternatives
      if … else if … else if … else if … else
                                CGS 3460


                         The else if Statement
   Format
     if ( condition1 )
         program statement 1;
     else if (condition2)         Condition 1    No
         program statement 2;     satisfied?

   Flow                                   Yes   Condition 2   No
                                                 satisfied?
                                   Program
                                   statement 1         Yes
                                                 Program
                                                 statement 2
                                    CGS 3460

               Consider assigning letter grades
   Have a floating point number between 0 and 100.
      Want to assign corresponding letter grade to number grade, x
        • A – if 90 ≤ x ≤100
        • B – if 80 ≤ x < 90
        • C – if 70 ≤ x < 80
        • D – if 60 ≤ x < 70
        • E – x < 60
                                                                           CGS 3460


   X ≥ 90
                 No
                                                                  An example
          Yes                          No
letGrade = ‘A’           X ≥ 80


                                Yes                          No
                      letGrade = ‘B’           X ≥ 70


                                                      Yes                          No
                                            letGrade = ‘C’           X ≥ 60


                                                                            Yes         letGrade = ‘E’
                                                                  letGrade = ‘D’
                                 CGS 3460

                         Assigning Letter Grades
if ( x >= 90 )
       letGrade = ‘A’;
else if (x >= 80)
       letGrade = ‘B’;
else if (x >= 70)
       letGrade = ‘C’;
else if (x >= 60)
       letGrade = ‘D’;
else
       letGrade = ‘E’

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:7
posted:3/1/2012
language:
pages:15