ekt120 week02 selection

Upload: muhammad-faiz-bin-ahmad-shafi

Post on 03-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Ekt120 Week02 Selection

    1/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 1

    Week 3Selection Structures

  • 8/12/2019 Ekt120 Week02 Selection

    2/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 2

    Outline Recall selection control structure Types of selection

    One-way selection Two-way selection Multi-selection Compound statement

    Nested if Conditional operator Switch structure

  • 8/12/2019 Ekt120 Week02 Selection

    3/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 3

    Recall..

    Selection Structure Used to choose among alternative

    courses of action

    C has three types: if, if..else, andswitch

  • 8/12/2019 Ekt120 Week02 Selection

    4/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 4

    The ifselection structure

    ifstructure is a single-entry/single-exit

    structure

    true

    false

    grade >= 60 print Pass

    If students grade is greater than or equal to 60

    Print Pass

  • 8/12/2019 Ekt120 Week02 Selection

    5/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 5

    The if..elseselection

    structure Specifies an action to be performed

    both when the condition is trueand

    when it is false

    truefalse

    print Fail print Pass

    grade >= 60

    If students grade is greater than

    or equal to 60

    print Pass

    else

    print Fail

  • 8/12/2019 Ekt120 Week02 Selection

    6/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 6

    Selection Statements Used to control the flow of a program

    Also called as decision orbranches

    Branches are conditions or choices usedto enable selection of program flow

  • 8/12/2019 Ekt120 Week02 Selection

    7/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 7

    Types of selection One-way selection = if

    Two-way selection = if..else

    Multi-selection

    Nested if

    Switch structure =switch

  • 8/12/2019 Ekt120 Week02 Selection

    8/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 8

    One-way Selection = if

    In C, a conditionis represented by a logical (Boolean)expression

    trueand falseare logical (Boolean) values

    The syntax of one-way selection is:

    if (expression) statement;

    If the value of the expression is true, statement is

    executed; if false, statement is not executed and the computer

    goes on to the next statement in the program.

  • 8/12/2019 Ekt120 Week02 Selection

    9/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 9

    One-way Selection = if

    true

    false

    grade >= 60 print Pass

    If students grade is greater than or equal to 60

    Print Pass

  • 8/12/2019 Ekt120 Week02 Selection

    10/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 10

    One-way Selection = if

    ..

    if(grade >= 60)

    printf(Pass);

    ..

    ..

  • 8/12/2019 Ekt120 Week02 Selection

    11/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 11

    One-way Selection = if

    Another example:

    char grade;

    if(markah>= 90)

    grade = 'A';

    ...

    printf(Grade is : %c\n, grade);

  • 8/12/2019 Ekt120 Week02 Selection

    12/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 12

    One-way Selection = if

    Another example:

    if (temperature is greater than 70 degree

    and it is not raining) recommended activity is golfing

    bool rain=false;

    if((temp > 70) && !(rain))

    printf(recommended activity is golfing);

  • 8/12/2019 Ekt120 Week02 Selection

    13/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 13

    One-way Selection = if

    Common Errors

    if score >= 90 //no parentheses

    grade = 'A';

    if(score >= 90);//; not here

    grade = 'A';

  • 8/12/2019 Ekt120 Week02 Selection

    14/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 14

    Two-way Selection =if..else

    The syntax of two-way selection is:

    if (expression)

    statement1;

    else

    statement2;

    If the value of the expression is true,statement1 is executed;

    if false, statement2 is executed

  • 8/12/2019 Ekt120 Week02 Selection

    15/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 15

    Two-way Selection =if..else

    truefalse

    print

    Fail

    print

    Pass

    grade >=

    60

    If students grade is greater than or equal

    to 60

    print Pass

    else

    print Fail

  • 8/12/2019 Ekt120 Week02 Selection

    16/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 16

    Two-way Selection =if..else

    if(grade >=60)

    printf(Pass);else

    printf(Fail);

  • 8/12/2019 Ekt120 Week02 Selection

    17/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 17

    Two-way Selection =if..else

    Another example:if (hour > 40.0) //Line 1

    wages = 40.0 * rate +1.5 * rate * (hour - 40.0); //Line 2

    else //Line 3

    wages = hour * rate; //Line 4

    If hour is 50, then the statement at Line 2is

    executed If hour is 30, then the statement at Line 4is

    executed

  • 8/12/2019 Ekt120 Week02 Selection

    18/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 18

    Multi-selection = if-else if

    The syntax is:

    if(exp1)

    stmt1;else if(exp2)

    stmt2;

    else if(exp3)

    stmt3;

    else

    stmt n;

    An if-else if control structureshifts program control, step by

    step, through a series of

    statement blocks.

  • 8/12/2019 Ekt120 Week02 Selection

    19/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 19

    Multi-selection = if-else if

    E.g.

    temp display

    >30 0c hot

    20-30 0c mild

    10-20 0c cold

    30 Print hottrue

    false

    temp > 20 Print mildtrue

    temp >10 Print cold

    Print very cold

    true

    false

    false

  • 8/12/2019 Ekt120 Week02 Selection

    20/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 20

    Multi-selection = if-else if

    if(temp > 30)printf( hot\n);

    else if((temp >=20) && (temp=10) && (temp < 20))

    printf(cold\n);else

    printf( very cold\n);

  • 8/12/2019 Ekt120 Week02 Selection

    21/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 21

    Compound (Block of) Statement A compound statement (also called a

    block of statements) takes the form of { statement 1; statement 2; . . .

    statement n; }

    It is considered a single statement

  • 8/12/2019 Ekt120 Week02 Selection

    22/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 22

    Compound (Block of) Statement

    Example:if (age > 18){

    printf("Eligible to vote\n);printf("No longer a minor\n);

    }else{

    printf("Not eligible to vote\n);printf(Still a minor\n);

    }

  • 8/12/2019 Ekt120 Week02 Selection

    23/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 23

    Nested if

    When one control statement is within another, it issaid to be nested

    if(exp1)

    if(exp2)statement1; OR

    if(exp1)

    {

    statement1;if(exp2)

    statement2;

    }

  • 8/12/2019 Ekt120 Week02 Selection

    24/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 24

    Nested if

    Example:if (temperature >= 50)

    {

    if (temperature >= 80)

    printf( "Good day for swimming.\n);

    else

    printf( "Good day for golfing.\n);

    }

    else

    printf("Good day to play tennis.\n);

  • 8/12/2019 Ekt120 Week02 Selection

    25/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 25

    Nested if

    Another example

  • 8/12/2019 Ekt120 Week02 Selection

    26/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 26

    The Conditional Operator (? :)

    The syntax of using the conditional operatoris: expression1 ? expression2 : expression3;

    This is called a conditional expression. The statement:

    if (a >= b)max = a;

    else

    max = b; Is equivalent to the statement:

    max = (a >= b) ? a : b;

  • 8/12/2019 Ekt120 Week02 Selection

    27/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 27

    switch Structures

    Similar to if-else if control structure The general form (syntax):

    switch (expression){

    case value1: statements1; break;case value2: statements2; break;..

    .case valuen: statementsn; break;default: statements;

    }

  • 8/12/2019 Ekt120 Week02 Selection

    28/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 28

    switch Structures

    The breakstatement has a specialmeaning and may or may not appearafter each statement.

    In C, switch, case, break, and defaultare reserved words.

    In a switchstructure, first the

    expression is evaluated. The value ofthe expression is then used to performthe corresponding action.

  • 8/12/2019 Ekt120 Week02 Selection

    29/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 29

    switch Structures

    The expression is usually an identifier.

    The value of the expression can be only integral.

    The expression is sometimes called the selector. Itsvalue determines which statement is selected forexecution.

    A particular casevalue should appear only once.

    One or more statements may follow a case label, so

    you do not need to use braces to turn multiplestatements into a single compound statement.

    The breakstatement may or may not appear aftereach statement.

  • 8/12/2019 Ekt120 Week02 Selection

    30/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 30

    switch Structures

    Example:switch (grade){

    case 'A': printf("The grade is A.); break;case 'B': printf("The grade is B.); break;case 'C': printf("The grade is C.); break;case 'D': printf("The grade is D.); break;case 'F': printf("The grade is F.); break;default: printf("The grade is invalid.);

    } where, grade is a variable of the type char. If the

    value of grade is, say 'A', the output isThe grade is A.

  • 8/12/2019 Ekt120 Week02 Selection

    31/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 31

    switch Structures

    The switch statement executes according to thefollowing rules: When the value of the expression is matched against a case

    value (also called a label), the statements execute untileither a break statement is found or the end of the switchstructure is reached.

    If the value of the expression does not match any of thecase values, the statements following the default labelexecute. If the switch structure has no default label, and if

    the value of the expression does not match any of the casevalues, the entire switch statement is skipped.

    A break statement causes an immediate exit from theswitch structure

  • 8/12/2019 Ekt120 Week02 Selection

    32/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 32

    Whats wrong??

  • 8/12/2019 Ekt120 Week02 Selection

    33/33

    UniMAP Sem II-09/10 EKT120: Computer Programming 33

    End Week 2

    Q & A!