1 / 26

MSAS5104 Programming with Data Structures and Algorithms

Learn about the different control structures in structured programming, including conditionals (if/else statements) and switch statements. Understand truth tables and compound statements. Includes lab exercises and homework topics.

rfrancis
Download Presentation

MSAS5104 Programming with Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MSAS5104Programming with Data Structures and Algorithms Week 5 Scott Marino Scott Marino MSMIS Kean University

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

  3. 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 Scott Marino MSMIS Kean University

  4. 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 Scott Marino MSMIS Kean University

  5. 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 Scott Marino MSMIS Kean University

  6. 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 Scott Marino MSMIS Kean University

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

  8. 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 Scott Marino MSMIS Kean University

  9. 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)) Scott Marino MSMIS Kean University

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

  11. 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 Scott Marino MSMIS Kean University

  12. 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) Scott Marino MSMIS Kean University

  13. 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 Conditionals - if Scott Marino MSMIS Kean University

  14. 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 Conditionals - if/else Scott Marino MSMIS Kean University

  15. 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 Scott Marino MSMIS Kean University

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

  17. 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);} Scott Marino MSMIS Kean University

  18. 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 Scott Marino MSMIS Kean University

  19. 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 Conditionals - Switch Scott Marino MSMIS Kean University

  20. 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 Scott Marino MSMIS Kean University

  21. Truth Tables • C1 C2 And Or !C1 !C2T T T T F FT F F T F TF T F T T FF F F F T T Scott Marino MSMIS Kean University

  22. 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 Scott Marino MSMIS Kean University

  23. 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 Scott Marino MSMIS Kean University

  24. 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 Scott Marino MSMIS Kean University

  25. 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” Scott Marino MSMIS Kean University

  26. 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 Scott Marino MSMIS Kean University

More Related