Problem Solving with Decisions PDF

Summary

This presentation provides a practical introduction to decision structures and their importance in programming, focusing on if statements, if-then-else constructs, and the case statement (which is also known as a switch statement). This document is an introduction to programming concepts. It includes multiple examples with different types of branching, and explains how to use Boolean logic and operators (e.g., AND and OR) within the context of making decisions in computer programs.

Full Transcript

CST8116 Intro. to Comp. Prog. Week 09 Lesson 01 Selection Structure (Decisions) Week 09 Lesson 01 Selection Structure (Decisions) Selection Structure, also called Decision Structure Single-alternative selection structure Dual-alternative selection structure N...

CST8116 Intro. to Comp. Prog. Week 09 Lesson 01 Selection Structure (Decisions) Week 09 Lesson 01 Selection Structure (Decisions) Selection Structure, also called Decision Structure Single-alternative selection structure Dual-alternative selection structure Nested Selection Structure Using AND and OR Testing Boundary Cases Using AND and OR together NOT operator (negative logic) Nested logic for ranges of values Selection Structure: Case 2 Welcome This presentation provides an introduction to the Selection Structure. 3 Selection Structure, also called Decision Structure The selection structure permits the program to make a decision, based on data and logic, resulting in different sequence structures to be executed. A selection structure starts with a boolean expression, and then takes one of two paths. Most programming languages will have selection structure, either if, if-then-else, and a case structure (called switch in Java). 4 Single-alternative selection structure A simple program can obtain a user input numeric value and determine if it is less than 10 providing output if this is true. start declarations num number output "enter a number" input number if number < 10 then output "number is less than 10" endif output "thank you for using the program" stop 5 Dual-alternative selection structure Determine if it is less than 10 or greater than 10: start declarations num number output "enter a number" input number if number < 10 then output "number is less than 10" else output "number is greater than 10" endif output "thank you for using the program" stop 6 What about 10? If you missed it, a value that is exactly 10 will be reported by the program as greater than 10. The program only checks for less than 10, otherwise it will report greater than 10, it does not address if the value is exactly 10. Either need to ask the client the program is for, if 10 needs to be in the lower- range, or upper-range i.e. use 10 then output "number is greater than 10" else output "number is equal to 10“ endif endif output "thank you for using the program" stop 8 Nested Selection Structure Alternative Solutions Alternative solutions are possible. Test your learning: Can you re-write the decision structure above to start with checking if the number equals 10 as well as draw a flowchart? Can you re-write the decision structure to start with checking if the number is greater than 10 as well as draw a flowchart? For this trivial example, there will not be (m)any changes to the structure itself, however there are cases where carefully choosing the order of the logic impacts the number of decisions needed to solve the problem. 9 Using AND and OR By using the AND, and OR operators we can make more complicated decisions. E.g. is a number is within a range of values, or outside a range of values. E.g. AND start declarations num number output "please enter a number" input number if number >= 1 AND number 42 then endif endif output "not 42" endif 14 Nested logic for ranges of values Where an if-statement is evaluated top-down sequentially, and only one branch of logic per if-else is processed, you do not need to check each range of values. Assume that there are letter grades with ranges at a school: Percent Grade Letter Grade Numeric Grade 80 – 100 A 4.0 70 – 79 B 3.0 60 – 69 C 2.0 50 – 59 D 1.0 0 – 49 F 0.0 To determine a student grade based on an entered integer number that falls between a range we could use a sequence of statements with AND logic, but should we or can things be simpler (see next slides) 15 Nested logic for ranges of values (AND not needed) if grade >= 80 AND grade = 70 AND grade = 60 AND grade = 50 AND grade = 80 then output "A" else if grade >= 70 then output "B" else if grade >= 60 then output "C" else if grade >= 50 then ouput "D" else output "F" endif endif endif endif If already know a value is not >= 80, no need to check again, just ask is it >= 70 and so on 17 Selection Structure: Case The case Structure (known as switch in Java) This is a specialized selection structure that matches a value against single values. Note: If a decision needs to be made based on a value exactly matching a set of values this is a good choice of decision structure However, if you need to make decisions based on ranges of values, the nested if-else is much better. Similar to else, you can provide a default branch of logic. 18 Selection Structure: Case Here is a simple example where a user enters one of two options in operating a program, based on numeric integer input. start declaration num userOption output "Please enter 1 or 2 for option" input userOption case userOption 1: output "abc" 2: output "xyz" default: output "invalid option selected" endcase stop Typically Magic Numbers, like 1 and 2 above, are replaced with constants e.g. OPTION_ABC, OPTION_XYZ 19 Conclusion In this lesson we reviewed: Structured Programming Overview Selection Structure, also called Decision Structure Single-alternative selection structure Dual-alternative selection structure Nested Selection Structure Using AND and OR Testing Boundary Cases Using AND and OR together NOT operator (negative logic) Nested logic for ranges of values Selection Structure: Case 20 References Joyce Farrell. 2018. Programming Logic & Design Comprehensive. 9th Ed. Cengage Learning. Chapter 4 pp. 124 to 167 Cay Horstmann. 2019. Big Java Early Objects. 7th Ed. Wiley. Chapter 5 pp. 131 to 170 21

Use Quizgecko on...
Browser
Browser