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
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:

COSC 181 Foundations of Computer Programming Class 9 Notice No Class on Wednesday February 11th Today Introduce some important concepts about control structures if else while Introduce the UML Activity Diagram Do some in class examples Welcome to Chapter 4 In 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 While writing a program Know what building blocks are available Use good programming principles Algorithms Have a thorough understanding of problem Carefully plan your approach for solving it 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 Similar to everyday English Only executable statements Easy to convert into C program No need to declare variables Not executed on computers Figure 4 1 pg 134 Control Structures Sequential execution Transfer of control Statements executed in sequential order 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 for More UML Use Cases Class Diagrams UML Activity Diagram Fig 4 2 Models the workflow Action state symbols Small circles Solid circle is the initial state Solid circle in a hollow circle is the final state Transition arrows Rectangles with curved sides Represent the flow of activity Comment notes Connected to diagram by dotted lines Control 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 nesting What 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 60 Print 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 path UML Activity Diagram w decision if else DoubleSelection Statement if if else Performs one action if condition is true a different action if it is false Pseudocode Performs action if condition true 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 Diagram Indentation 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 DoubleSelection 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 false Nesting 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 beginning A 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 statement More 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 Without braces cout You must take this course again n always executes Good Programming Practice Always include the braces


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 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?