Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22COSC 181 – Foundations of Computer ProgrammingClass 9Notice!No Class on Wednesday, February 11thTodayIntroduce some important concepts about “control structures”if-elsewhileIntroduce the UML Activity DiagramDo some in-class examplesWelcome to Chapter 4In this chapter you will learn: Basic problem-solving techniques.To develop algorithms through the process of top-down, stepwise refinement.To use the if and if...else selection statements to choose among alternative actions.To use the while repetition statement to execute statements in a program repeatedly.Counter-controlled repetition and sentinel-controlled repetition.To use the increment, decrement and assignment operators.Before writing a programHave a thorough understanding of problem Carefully plan your approach for solving itWhile writing a program Know what “building blocks” are availableUse good programming principlesAlgorithmsThe actions to executeThe order in which these actions executeProgram controlSpecifies the order in which actions execute in a programPerformed in C++ with control statementsPseudocodeArtificial, informal language used to develop algorithmsUsed to think out program before codingEasy to convert into C++ programSimilar to everyday EnglishOnly executable statementsNo need to declare variablesNot executed on computers Figure 4.1, pg 134Control StructuresSequential executionStatements executed in sequential orderTransfer of controlNext statement executed is not the next one in sequenceThree control structures Sequence structurePrograms executed sequentially by defaultSelection structures•if, if…else, switch Repetition structures•while, do…while, forMore UMLUse CasesClass DiagramsUML Activity Diagram (Fig. 4.2)Models the workflowAction state symbolsRectangles with curved sidesSmall circlesSolid circle is the initial stateSolid circle in a hollow circle is the final stateTransition arrowsRepresent the flow of activityComment notesConnected to diagram by dotted linesControl Structures (Con’t)Single-entry/single-exit control statements Three types of control statementsSequence statementSelection statementsRepetition statementsCombined in one of two waysControl statement stackingConnect exit point of one to entry point of the nextControl statement nestingWhat Constitutes Code?Any C++ program we will ever build can be constructed from only seven different types of control statements (sequence, if, if... else, switch, while, do... while and for) combined in only two ways (control-statement stacking and control-statement nesting).Review – The if StatementChoose among alternative courses of actionPseudocode exampleIf student’s grade is greater than or equal to 60Print “Passed”If the condition is truePrint statement executes, program continues to next statementIf the condition is falsePrint statement ignored, program continuesIndenting makes programs easier to readC++ ignores white-space charactersTranslation into C++•if ( grade >= 60 ) cout << "Passed";Consistently applying reasonable indentation conventions throughout your programs greatly improves program readability.Activity Diagram with if statmentDiamond symbol in UML modelingIndicates decision is to be madeContains guard conditionsTest conditionFollow correct pathUML Activity Diagram w/ decisionif…else Double-Selection StatementifPerforms action if condition trueif…elsePerforms one action if condition is true, a different action if it is falsePseudocodeIf student’s grade is greater than or equal to 60 print “Passed”Else print “Failed” C++ code•if ( grade >= 60 ) cout << "Passed";else cout << "Failed";Updated Activity DiagramIndentation PracticesIndent both body statements of an if... else statement.if ( grade >= 60 ){ cout << "Passed"; cout << “ Good Job” << endl; }else{ cout << "Failed"; cout << “ Try Again” << endl; }Another Double-Selection StatementTernary conditional operator (?:)Three arguments (condition, value if true, value if false)Code could be written:cout << ( grade >= 60 ? “Passed” : “Failed” );Condition Value if true Value if falseNesting Selection StatementsNested if…else statementsOne inside another, test for multiple cases Once a condition met, other statements are skippedExampleIf student’s grade is greater than or equal to 90 Print “A” Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else Print “F”If…else continued if ( studentGrade >= 90 ) cout << "A";else if (studentGrade >= 80 ) cout << "B";else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D";else cout << "F";A nested if...else statement can perform much faster than a series of single-selection if statements because of the possibility of early exit after one of the conditions is satisfied.Even faster on average if we test the conditions we think are most likely to be true at the beginningA Common MistakeDangling-else problemCompiler associates else with the immediately preceding ifExample•if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5";else cout << "x is <= 5";Compiler interprets as•if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5";How do we fix this?Answer:Rewrite with braces ({})•if ( x > 5 ){ if ( y > 5 ) cout << "x and y are > 5";}else cout << "x is <= 5";Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statementMore on If - ElseCompound statementAlso called a blockSet of statements within a pair of bracesUsed to include multiple statements in an if bodyExample•if ( studentGrade >= 60 ) cout << "Passed.\n";else { cout << "Failed.\n"; cout << "You must take this course again.\n";}
View Full Document