ekt120 week03 selection

33
UniMAP Sem II- 11/12 EKT120: Computer Programming 1 Week 3 – Selection Structures

Upload: tan-shouzheng

Post on 27-Sep-2015

228 views

Category:

Documents


0 download

DESCRIPTION

Selection Programming

TRANSCRIPT

  • UniMAP Sem II-11/12EKT120: Computer Programming*Week 3 Selection Structures

    EKT120: Computer Programming

  • EKT120: Computer Programming*OutlineRecall selection control structureTypes of selectionOne-way selectionTwo-way selectionMulti-selectionCompound statementNested ifConditional operatorSwitch structureUniMAP Sem II-11/12

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Recall.. Selection StructureUsed to choose among alternative courses of actionC has three types: if, if..else, and switch

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*The if selection structureif structure is a single-entry/single-exit structure

    If students grade is greater than or equal to 60Print Pass

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*The if..else selection structureSpecifies an action to be performed both when the condition is true and when it is false

    If students grade is greater than or equal to 60 print Passelse print Fail

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Selection StatementsUsed to control the flow of a programAlso called as decision or branches Branches are conditions or choices used to enable selection of program flow

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Types of selectionOne-way selection = ifTwo-way selection = if..elseMulti-selectionNested ifSwitch structure = switch

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = ifIn C, a condition is represented by a logical (Boolean) expressiontrue and false are logical (Boolean) valuesThe 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.

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = if

    If students grade is greater than or equal to 60Print Pass

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = if..if(fGrade >= 60)printf(Pass);....

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = ifAnother example: char cGrade;if(fMarkah>= 90) cGrade = 'A'; ...printf(Grade is : %c\n, cGrade);

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = ifAnother example:if (temperature is greater than 70 degree and it is not raining) recommended activity is golfing

    bool rain=false;if((fTemp > 70) && !(rain))printf(recommended activity is golfing);

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*One-way Selection = ifCommon Errors if fScore >= 90 //no parentheses cGrade = 'A'; if(fScore >= 90); //; not herecGrade = 'A';

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Two-way Selection = if..elseThe syntax of two-way selection is:

    if (expression) statement1; elsestatement2;

    If the value of the expression is true, statement1 is executed; if false, statement2 is executed

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Two-way Selection = if..else If students grade is greater than or equal to 60 print Passelse print Fail

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Two-way Selection = if..else if(fGrade >=60)printf(Pass);else printf(Fail);

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Two-way Selection = if..elseAnother example: if (fHour > 40.0)//Line 1fWages = 40.0 * fRate +1.5 * fRate * (hour - 40.0);//Line 2else//Line 3 fWages = fHour * fRate;//Line 4 If fHour is 50, then the statement at Line 2 is executedIf fHour is 30, then the statement at Line 4 is executed

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Multi-selection = if-else ifThe syntax is:if(exp1)stmt1;else if(exp2)stmt2;else if(exp3)stmt3;elsestmt n;An if-else if control structure shifts program control, step by step, through a series of statement blocks.

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Multi-selection = if-else ifE.g. fTemp >30 Print hottruefalsefTemp > 20Print mildtruefTemp >10Print coldPrint very coldtruefalsefalse

    tempdisplay>30 0chot20-30 0cmild10-20 0ccold

  • UniMAP Sem II-11/12EKT120: Computer Programming*Multi-selection = if-else ifif(fTemp > 30)printf( hot\n);else if((fTemp >=20) && (fTemp=10) && (fTemp < 20))printf(cold\n);elseprintf( very cold\n);

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Compound (Block of) StatementA compound statement (also called a block of statements) takes the form of{ statement 1; statement 2; . . . statement n; } It is considered a single statement

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Compound (Block of) StatementExample:if (iAge > 18) { printf("Eligible to vote\n);printf("No longer a minor\n); } else { printf("Not eligible to vote\n);printf(Still a minor\n);}

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Nested ifWhen one control statement is within another, it is said to be nested if(exp1)if(exp2) statement1; ORif(exp1){statement1;if(exp2) statement2;}

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Nested ifExample:if (fTemperature >= 50) {if (fTemperature >= 80) printf( "Good day for swimming.\n);elseprintf( "Good day for golfing.\n); }elseprintf("Good day to play tennis.\n);

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Nested ifAnother example#include void main (void){int iDay;float fTime;

    printf ("Type the day and time of interest\n\n");scanf (" %d %f ", &iDay, &fTime);

    if (iDay

  • UniMAP Sem II-11/12EKT120: Computer Programming*The Conditional Operator (? :)The syntax of using the conditional operator is: 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;

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*switch StructuresSimilar to if-else if control structureThe general form (syntax):switch (expression) { case value1: statements1; break; case value2: statements2; break; . . . case valuen: statementsn; break; default: statements; }

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*switch StructuresThe break statement has a special meaning and may or may not appear after each statement. In C, switch, case, break, and default are reserved words. In a switch structure, first the expression is evaluated. The value of the expression is then used to perform the corresponding action.

    EKT120: Computer Programming

  • The expression is usually an identifier. The value of the expression can be only integral. The expression is sometimes called the selector. Its value determines which statement is selected for execution. A particular case value should appear only once. One or more statements may follow a case label, so you do not need to use braces to turn multiple statements into a single compound statement. The break statement may or may not appear after each statement.

    UniMAP Sem II-11/12EKT120: Computer Programming*switch Structures

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*switch StructuresExample:switch (cGrade) { 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, cGrade is a variable of the type char. If the value of cGrade is, say 'A', the output is The grade is A.

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*switch StructuresThe switch statement executes according to the following rules: When the value of the expression is matched against a case value (also called a label), the statements execute until either a break statement is found or the end of the switch structure is reached. If the value of the expression does not match any of the case values, the statements following the default label execute. If the switch structure has no default label, and if the value of the expression does not match any of the case values, the entire switch statement is skipped. A break statement causes an immediate exit from the switch structure

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*Whats wrong??

    EKT120: Computer Programming

  • UniMAP Sem II-11/12EKT120: Computer Programming*End Week 2Q & A!

    EKT120: Computer Programming