CZ1102 Computing & Problem Solving Lecture 3

Embed Size (px)

Citation preview

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    1/25

    Relationoperationandconditional

    statement

    Week4

    1

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    2/25

    Logicalexpression Evaluatethecorrectnessofthestatement

    TRUE

    FALSE

    Sixrelationshipoperator

    1. == equal

    2. ~= notequal

    3. > greaterthan

    4. >= greaterthanorequalto

    5.

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    3/25

    (cont) Examples

    b2

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    4/25

    Logicaloperator

    logicaloperators

    ~:

    not&:and

    |: or

    Example>>1&0

    ans =

    0

    >>

    4

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    5/25

    Operatorprecedence

    Precedence(fromthefirsttothelast)

    Parenthesis

    () Arithmeticoperators

    Relationshipoperators

    ==,~=,>,>=,

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    6/25

    (cont)

    Example

    >>2>1&0ans=

    0

    Danger

    0

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    7/25

    (cont) Wrongway

    >>0(0

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    8/25

    AssignmentofLogicalexpression

    Example

    x

    =

    4

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    9/25

    Conditionalstatement

    Decisionexample1

    Considerthefollowingarithmeticoperationonx

    The aboveoperationmustdecidewhetherornotx

    ispositiveornot.Theifconstruct,whichisfundamentaltoallcomputinglanguages,isthebasis

    ofsuchdecisionmaking.

    9

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    10/25

    Theonelineif statement ifcondition

    statement

    end

    conditionisusuallyalogicalexpression Ifconditionistrue,statementisexecuted,butifconditionisfalse,

    nothinghappens.

    Samplecode

    >>x=3>>if(x>xx=

    3

    10

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    11/25

    Decisionexample2 Example

    Considerthefollowingarithmeticoperationonx

    1, 01, ifcondition

    statementA

    elsestatementB

    end

    conditionisusuallyalogicalexpression Ifconditionistrue,statementA isexecuted,butifconditionisfalse,statementB isexecuted. Dontforgetend,orMATLABwillwaitforever.

    11

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    12/25

    Samplecode Thecode

    >>x=3

    >>if(x>=0)

    x=1;

    else

    x=1;

    end

    >>x

    x=

    1

    12

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    13/25

    elseif ladder ifcondition1

    statementsA

    elseifcondition2

    statementsB

    elseifcondition3

    statementsC

    else

    statementsE

    end

    13

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    14/25

    Howdoesitwork condition1istested.Ifitistrue,statementsA areexecuted;MATLAB

    thenmovestothenextstatementafterend.

    Ifcondition1isfalse,MATLABcheckscondition2.Ifitistrue,statementsB

    areexecuted,followedbythestatementafterend.

    Inthisway,alltheconditionsaretesteduntilatrueconditionisfound.

    Assoonasatrueconditionisfound,nofurtherelseifs areexamined,

    and MATLABjumpsofftheladder.

    Ifnoneoftheconditionsistrue,statementsEafterelseareexecuted.

    Youshouldarrangethelogicsothatnotmorethanoneofthe

    conditions istrue.

    Therecanbeanynumberofelseifs,butatmostoneelse.

    elseifmustbewrittenasoneword.14

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    15/25

    Example Code

    >>if(x>0)

    x=1;

    elseif (x=0)

    x=0;else

    x=1;

    end Whatthisconditionalstatementdoes?

    15

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    16/25

    Multipleifsvs elseif Casestudy:Theinterestrateofthebankisgivenas

    following

    Ifsavingisbetween[0,5000),rateis0.01

    Ifsavingisbetween[5000,10000),rateis0.015

    If

    saving

    >=10000,

    rate

    is

    0.02.

    16

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    17/25

    Samplecode>>ifsaving

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    18/25

    Wrongcodeusingmultipleifs>>ifsaving

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    19/25

    Correctoneusingmuiple ifs.>>ifsaving=5000&saving=10000;

    rate

    =0.02;end

    19

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    20/25

    Nestedifs Anifconstructcancontainfurtherifs,andsoon.

    ifcondition1

    statementsA

    ifcondition2statementsB

    elsestatementsC

    end

    else

    statementD

    end

    20

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    21/25

    Example Example

    Find

    one

    real

    root

    of

    ,

    Thesolutionis

    If c/b

    Otherwise

    If 4 0, (or

    ) Otherwise,complexrootsonly

    21

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    22/25

    Samplecode >>if(a==0)

    x= b/c;

    else

    if(b^24*a*c>0)

    x=(b+sqrt(b^24*a*c)/(2*a);

    elsedisp (norealroot);

    end;

    end;

    Itisgoodprogrammingstyletoindenteachgroupofstatementsasshown.

    22

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    23/25

    Switchstruct Theswitch statementexecutescertainstatementsbasedonthevalueofa

    variableorexpression.

    switch Variablecase valueA

    statementA;

    case valueB

    statementB0;

    casevalueEstatementE;

    otherwise

    statementF;

    end

    23

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    24/25

    Switchstruct Example:Rollingdiceincasino,

    24

  • 8/8/2019 CZ1102 Computing & Problem Solving Lecture 3

    25/25

    Usingswitch

    struct

    Samplecode

    >> D%denotethedicenumber

    switchD

    case1pay=50;

    case2

    pay=20;

    case3

    pay=10;case4

    pay=5;

    case5

    pay=10;

    otherwisepay=25;

    end

    25