lecture #6 selection control structures cs602 (1)

Upload: bassantmohamad

Post on 02-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    1/24

    Lecture 6Selection Control Structures

    C Programming

    1

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    2/24

    Lecture 6 Topics

    Nested Structures

    Switch statement

    2

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    3/24

    Multi-alternative Selection is also called multi-way branching , and can beaccomplished by using NESTED if statements.

    3

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    4/24

    Nested if Statements if ( Expression1 )

    Statement 1

    else if ( Expression2 ) Statement 2

    .

    .

    .

    else if ( ExpressionN )Statement N

    else Statement N+1

    EXACTLY 1 of these statements will be executed. 4

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    5/24

    Nested if StatementsEach Expression is evaluated in sequence, until some Expression is found that is true .

    Only the specific Statement following that particulartrue Expression is executed.

    If no Expression is true , the Statement following thefinal else is executed.

    Actually, the final else and final Statement areoptional . If omitted, and no Expression is true, thenno Statement is executed.

    AN EXAMPLE . . .5

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    6/24

    Multi-way Branchingif ( creditsEarned >= 90 )

    printf ( Fourth year student ) ;

    else if ( creditsEarned >= 60 )

    printf ( Third year student ) ;

    else if ( creditsEarned >= 30 )

    printf ( Second year student ) ;else

    printf ( First year student ) ;

    6

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    7/24

    Example 1Display one word to describe the int value of

    number as Positive , Negative , or Zero

    7

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    8/24

    Answer

    if (number > 0)printf ( Positive );

    else if (number < 0)

    printf ( Negative );

    else

    printf ( Zero ) ;

    8

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    9/24

    Example 2Your city classifies a pollution index:-

    less than 35 as Pleasant ,35 through 60 as Unpleasant , and above 60 as Health Hazard.

    Display the correct description of the pollution

    index value.

    9

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    10/24

    Answer if ( index < 35 )

    printf ( Pleasant );

    else if ( index

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    11/24

    Example 3Every Sunday thru Thursday you go toclass. When it is raining you take an umbrella.

    But on the weekend, what you do dependson the weather.

    If it is raining you read in bed.Otherwise, you have fun outdoors.

    11

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    12/24

    // program tells how to spend your day #include < stdio.h >

    void main ( ){ int day;

    char raining;

    printf( Enter day (use 1 for Sunday) ) ;scanf( %d , &day ) ;

    printf( Is it raining? (Y/N) ) ;scanf( %c , &raining );

    12

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    13/24

    if ( ( day == 6) || (day == 7) ) /* Fri or Sat */ { if (raining == Y)

    printf( Read in bed );

    elseprintf( Have fun outdoors );

    }

    else{ printf( Go to class );if (raining == Y)

    printf( Take an umbrella );}

    } /* End of Program */

    13

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    14/24

    In the absence of braces, an else is alwayspaired with the closest preceding if that doesntalready have an else paired with it.

    14

    Note on pairing else with if

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    15/24

    Bad Example has output: FAIL float average;

    average = 100.0;

    if ( average >= 60.0 )

    if ( average < 70.0 )printf( Marginal PASS );else

    printf( FAIL );

    WHY? The compiler ignores indentation and pairsthe else with the second if.

    average

    100 0

    15

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    16/24

    To correct the problem, use braces

    float average;

    average = 100.0;

    if ( average >= 60.0 )

    {if ( average < 70.0 )

    printf ( Marginal PASS );

    }else

    printf ( FAIL ) ;

    average

    100 0

    16

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    17/24

    Switch statement

    Used to select one of several alternatives

    BASED on

    the

    value

    of

    a single

    variable.

    This variable may be an int or a char but NOT a float ( or double).

    17

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    18/24

    The switch Statementswitch (expression) { case constantl : statement 1; break ; case constant2:

    statement2;

    break; case constant3: statement3; break; case constantX: statementX; break; default : default statement; }

    18

    Multiple cases with same statements are acceptedIf break is omitted from a case, and the program is executing its statements , itwill continue to execute other cases until a break is metdefault can appear in any order

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    19/24

    Examplechar grade ;

    printf(Enter your letter grade: );

    scanf(%c, &grade);

    switch ( grade )

    { case A : printf( Excellent Job);

    break;

    case B : printf ( Very Good );

    break; case C : printf( Not bad );

    break;

    case F

    : printf(Faiing);

    break;

    default : printf( Wrong Input );

    }

    19

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    20/24

    Light bulbs

    Write a program to ask the user for the

    brightness of a light bulb (in Watts), and print

    out the expected lifetime:

    Brightness Lifetime in hours 25 2500

    40, 60 1000 75, 100 750

    otherwise 0 20

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    21/24

    int bright ;printf(Enter the bulb brightness: );scanf(%d, &bright);switch ( bright ) {

    case 25 : printf( Expected Lifetime is 2500 hours);break;case 40 :case 60 : printf ( Expected Lifetime is 1000 hours );

    break;case 75 :case 100 : printf(Expected Lifetime is 750 hours );

    break;default : printf(Wrong Input );

    }

    21

    lightBulb.c

    Hint :case 25+2 is accepted also bur case 25+x is not valid

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    22/24

    break vs

    return

    break means exit the switch statement and continue on with the rest of the program.

    return means exit the whole program.

    They could both be used anywhere in the program.

    22

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    23/24

    Testing Selection Control Structures

    to test a program with branches, use enoughdata sets so that every branch is executed atleast once

    this is called minimum complete coverage

    23

  • 8/10/2019 Lecture #6 Selection Control Structures CS602 (1)

    24/24

    How to Test a Program

    Design and implement a test plan A test plan is a document that specifies the test

    cases to try, the reason for each, and theexpected output

    implement the test plan by verifying that theprogram outputs the predicted results

    24