sheet_03_ver13-a_selection structures and switch statements (1)

Upload: sara-el-gendy

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Sheet_03_ver13-A_Selection Structures and Switch Statements (1)

    1/4

    Version 13a 1

    Department: Computer Engineering (Cairo Branch)Course Code:CC112Course : Structured ProgrammingSheet: 3Topics:Selection structures and switch statement

    ____________________________________________________________

    Level 1:

    Topic Program

    If statement 1) Find the error in the following statements

    If a == 5printf (Hello);

    If ( a < b );printf (ok);

    If ( x = y )printf (equal);

    If ( t < r)x = 2 ;

    t = r ;

    else

    printf

    Float x;x = 2 ;

    switch (x)

    {

    case ( 2 )

    printf(Hello);

    break;

    }

    Switch 2) What is the purpose of the default part of the switch command

    Selection 3) The else and default parts of selection statements, are they optional or mandatory?

    4) Write a program that takes two integers and displays their minimum.

    Level 2:Topic Program

    Tracing 5) Find the value of x for each of the following program segmentsa) Program segment 1

    int x = 2 ;if ( x >= 0 )

    x++;else

    if ( x >= 1 )x += 10;

    printf ( %d, x ) ;

    b) Program segment 2int x = 2 ;if ( x >= 0 )

    COLLEGE OF ENGINEERING & TECHNOLOGY

  • 7/30/2019 Sheet_03_ver13-A_Selection Structures and Switch Statements (1)

    2/4

    Version 13a 2

    x++;if ( x >= 1 )

    x += 10 ;printf ( %d, x ) ;

    6) What will be the value of x when y=12.0a) Program segment 1

    float x = 25.0, y ;scanf ( %d, &y ) ;

    if ( y != (x - 10.0 ) )x = x - 10.0 ;

    elsex = x / 2.0 ;

    printf ( x = %f, y = %f \n, x , y ) ;

    b) Program segment 2float x = 25.0, y ;scanf ( %d, &y ) ;if ( y < 12.0 )

    if ( y >= 0.0 )x = 5 * y ;

    elsex = 2 * y ;

    elsex = 3 * y ;

    printf ( x=%f,y=%f\n, x, y);

    Tracing /switch

    7) What will be the value of y after the following switch statement is executed?x = 3 ;

    switch (x+3){

    case 6: y = 1 ;default: y += 1 ;

    }

    Switch 8) Use a switch statement to rewrite the following if statementif ( a == 1 )

    x += 5 ;else if ( a == 2 )

    x += 10 ;else if ( a == 3 )

    x += 16 ;

    else if ( a == 4 )x += 34 ;

    If 9) Write a nested if statement to display a message that indicates the educational level of astudent based on his or her number of schooling:

    0-none1 through 5-Elementary School6 through 8-Middle School9 through 12-High School>12- College.

    Print a message to indicate invalid data as well.

    10) Write a program that may be used to compute the area of a square (area=side2) or a triangle(area=0.5*base*height) after prompting the user to type the first character of the figurename (S or T). Enter required data according to the figure being handled.

  • 7/30/2019 Sheet_03_ver13-A_Selection Structures and Switch Statements (1)

    3/4

    Version 13a 3

    Selection 11) Write a C program read the student score and display the corresponding grade according tothe following table:

    Score (x) Grade

    85

  • 7/30/2019 Sheet_03_ver13-A_Selection Structures and Switch Statements (1)

    4/4

    Version 13a 4