DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

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 11thTodayIntroduce some important concepts about “control structures”if-elsewhileIntroduce the UML Activity DiagramDo 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 programHave a thorough understanding of problem Carefully plan your approach for solving itWhile writing a program Know what “building blocks” are availableUse good programming principlesAlgorithmsThe actions to executeThe order in which these actions executeProgram controlSpecifies the order in which actions execute in a programPerformed in C++ with control statementsPseudocodeArtificial, informal language used to develop algorithmsUsed to think out program before codingEasy to convert into C++ programSimilar to everyday EnglishOnly executable statementsNo need to declare variablesNot executed on computers Figure 4.1, pg 134Control StructuresSequential executionStatements executed in sequential orderTransfer of controlNext statement executed is not the next one in sequenceThree control structures Sequence structurePrograms executed sequentially by defaultSelection structures•if, if…else, switch Repetition structures•while, do…while, forMore UMLUse CasesClass DiagramsUML Activity Diagram (Fig. 4.2)Models the workflowAction state symbolsRectangles with curved sidesSmall circlesSolid circle is the initial stateSolid circle in a hollow circle is the final stateTransition arrowsRepresent the flow of activityComment notesConnected to diagram by dotted linesControl Structures (Con’t)Single-entry/single-exit control statements Three types of control statementsSequence statementSelection statementsRepetition statementsCombined in one of two waysControl statement stackingConnect exit point of one to entry point of the nextControl 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 StatementChoose among alternative courses of actionPseudocode exampleIf student’s grade is greater than or equal to 60Print “Passed”If the condition is truePrint statement executes, program continues to next statementIf the condition is falsePrint statement ignored, program continuesIndenting makes programs easier to readC++ ignores white-space charactersTranslation into C++•if ( grade >= 60 ) cout << "Passed";Consistently applying reasonable indentation conventions throughout your programs greatly improves program readability.Activity Diagram with if statmentDiamond symbol in UML modelingIndicates decision is to be madeContains guard conditionsTest conditionFollow correct pathUML Activity Diagram w/ decisionif…else Double-Selection StatementifPerforms action if condition trueif…elsePerforms one action if condition is true, a different action if it is falsePseudocodeIf 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 PracticesIndent 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 StatementTernary 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 StatementsNested if…else statementsOne inside another, test for multiple cases Once a condition met, other statements are skippedExampleIf 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 MistakeDangling-else problemCompiler associates else with the immediately preceding ifExample•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 - ElseCompound statementAlso called a blockSet of statements within a pair of bracesUsed to include multiple statements in an if bodyExample•if ( studentGrade >= 60 ) cout << "Passed.\n";else { cout << "Failed.\n"; cout << "You must take this course again.\n";}


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?