Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino.

Similar presentations


Presentation on theme: "Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino."— Presentation transcript:

1 Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino

2 MSMIS Kean University Topics Structured Programming Conditionals If / Else Statements Switch Statements Truth Tables Compound Statements Lab 3 / Homework

3 Scott Marino MSMIS Kean University Structured Programming An understanding of structured programming techniques simplifies the programming process There are 3 control structures (constructs) available to every programming language All programs can be written using some combinations of those constructs

4 Scott Marino MSMIS Kean University Structured Programming The control structures are: –Sequence Operations Instructions performed one after another –Selection Operations Choosing a course of action –Iteration Operations Repeating a set of operations multiple times

5 Scott Marino MSMIS Kean University Structured Programming Most programmers start with a task to be completed –Weekly Payroll The task is divided into smaller subject areas that more clearly define the processes that must be done –Input time cards, compute paychecks, produce reports –These are high-level subject areas and are usually not detail oriented –Typically 3-7 tasks at each level

6 Scott Marino MSMIS Kean University Structured Programming Each task is further subdivided until a clear understanding of the task emerges –Compute paychecks consists of computing gross pay, deductions, and net pay The step-wise refinement continues until all the details about each task emerge –Deductions can be further broken down into federal tax, state tax, local tax, FICA, etc. The result is a top-down structure that can be translated into an outline

7 Scott Marino MSMIS Kean University Structured Programming Sequence Structure –The sequence structure consists of executing one instruction after another Input time cards Compute paychecks Print Paychecks Produce reports

8 Scott Marino MSMIS Kean University Structured Programming The high-level sequence structure can be expanded –Input time cards –Compute paychecks (expanded) Compute gross pay Compute deductions Compute net pay Store the information –Print Paychecks –Produce reports

9 Scott Marino MSMIS Kean University Structured Programming The selection structure is used to conditionally execute selected sequence instructions When computing gross pay, an employee may have worked overtime which may be paid at a different rate –Two sets of sequence instructions may be required One to calculate hours * pay rate One to calculate (40 hours * pay rate) + ((total hours - 40)) * (pay rate * 1.5))

10 Scott Marino MSMIS Kean University Structured Programming Using pseudo-code, the lower level tasks are more clearly defined to include any specific branches if hours > 40 calculate over-time pay else calculate regular pay end-if Use indentation to highlight the structure of the sequence

11 Scott Marino MSMIS Kean University Conditionals A condition is used by the program to execute a specific branch of sequence instructions A condition is typically structured as: –expression comparison expression An expression is typically a variable, a constant, or a calculated statement The comparisons are typically of a relational or equality nature

12 Scott Marino MSMIS Kean University Conditionals Equality comparisons –== (Equal) Note the double equal sign, single equal signs are for assignment and usually evaluate to true –!= (Not Equal) The exclamation point negates the condition Relational comparisons –> (greater than) –< (less than) –>= (greater than or equal to) –<= (less than or equal to)

13 Scott Marino MSMIS Kean University Conditionals - if Syntax: if (condition) { statement; … statement; } If the condition evaluates to true, execute all the statements inside the curly braces If the condition evaluates to false, bypass all the statements inside the curly braces

14 Scott Marino MSMIS Kean University Conditionals - if/else The if/else statement Syntax: if (condition) { statements; } else { statements; } If the condition evaluates to true execute all the statements inside curly braces before the else If the condition evaluates to false, execute all the statements inside the curly braces after the else

15 Scott Marino MSMIS Kean University Conditionals - if/else Nested if statements –An if condition can be nested inside another if condition Each else is “attached” to the nearest unmatched if statement Safe coding practice is to align the curly braces and to indent each level of the nested if statement to allow for easier reading

16 Scott Marino MSMIS Kean University Conditionals - Switch How to handle interrogating a single variable for multiple values if (myvar = “A”) do something; else if (myvar = “B”) do some other thing; else if (myvar = “C”) do something different; …

17 Scott Marino MSMIS Kean University Conditionals - Switch The switch statement is used to simplify interrogating a single variable for multiple values –switch (integralexpression) { case integralvalue: statement(s); break; case integralvalue: statement(s); break; default: statement(s); }

18 Scott Marino MSMIS Kean University Conditionals - Switch An integralexpression is typically the single variable name that would be the subject of the multiple if statements An integralvalue is value in the “constant” condition in the multiple if statements The break statement is used to exit the switch –If’s can be inside a switch statement and can be used to break early The default section of the switch statement handles any values not matching any case

19 Scott Marino MSMIS Kean University Conditionals - Switch switch (myvar) { case “A”: do something; case “B”: do some other thing; case “C”: something different; … default: cout << “Oops”; } The switch statement simplifies the “ugly” nesting of compound if statements

20 Scott Marino MSMIS Kean University Truth Tables Truth tables are used to determine the true / false state of compound conditions A compound condition includes 2 or more comparisons The conditions are compounded by “and” and “or” conditionals The “not” operator is a unary operator that changes true to false and false to true

21 Scott Marino MSMIS Kean University Truth Tables C1 C2 And Or !C1 !C2 T T T T F F T F F T F T F T F T T F F F F F T T

22 Scott Marino MSMIS Kean University Truth Tables With an “and” condition, both conditions must be true for a true result With an “or” condition, as long as one condition is true, the statement will evaluate to true –Many compilers stop evaluating an “or” condition after the first true condition is encountered The “not” operator negates the true or false result of a condition

23 Scott Marino MSMIS Kean University Compound Statements In C++ –The notation for “and” is && (double ampersands) –The notation for “or” is || (double pipes) –The notation for “not” is ! (exclamation point) –Parenthesis can be used to group or separate the evaluation of compound conditions Multiple nesting of parenthesis combined with indentation is recommended

24 Scott Marino MSMIS Kean University Compound Statements Syntax: –if (color == “Red” && roof == “Convertible”) cout << “Cool summer car\n”; –if (color == “Red” && (roof == “Convertible” || roof == “t-tops” || roof == “sunroof”)) { cout << “Cool summer car\n”; } // end if Indentation will make reading compound statements simpler

25 Scott Marino MSMIS Kean University Homework Enhance the Celsius / Fahrenheit conversion program –Have the program prompt for a C -> F or F -> C conversion and convert the temperature as required –The prompt should indicate the expected values for “convert from” –prompt>Enter the convert from temperature type [F,C] –If the result is below freezing or above boiling the result should print an extra message of “That’s cold” or “That’s hot”

26 Scott Marino MSMIS Kean University Homework Show test results for 0, 50, and 100 degrees Celsius and 31, 100, 212, and 213 degrees Fahrenheit plus 3 other temperatures of your choosing


Download ppt "Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 5 Scott Marino."

Similar presentations


Ads by Google