Chapter 5 Decisions
ssential uestion: How are Boolean expressions or operators used in everyday life?
Terms branch condition error-checking nesting selection sequence
Making Decisions in Code Section 5.1 Making Decisions in Code
Section 5.1 Learning Goal Compare ways to control how a program runs.
Making Decisions in Code Three ways to control how programs run Sequence: program will perform the first line of code first Selection: program can make decisions on which lines of code to execute Repetition: loops
Making Decisions in Code IF…THEN, IF…THEN…ELSE blocks Make decisions, control actions in programs Evaluates expressions (true or false) Block combination provides the program’s logic Operators category: comparison operators, also known as relational operators Result is always true or false
Making Decisions in Code Goodheart-Willcox Publisher IF…THEN and IF…THEN…ELSE statements are used to make decisions and reactions in computer programming.
Making Decisions in Code Condition: expression that can only be either true or false (yes or no) Called Boolean expressions Uses binary logic since there are only two possible outcomes Examples Home alarm systems Car’s lights Bouncing sprite off stage’s edge
Section 5.1 Review When would selection program control be used over sequence program control? Why is a condition called a Boolean expression?
Building Boolean Expressions Section 5.2 Building Boolean Expressions
Section 5.2 Learning Goal Apply operators to Boolean expressions.
Identifying Boolean Expressions Boolean expressions make decisions Look at the algorithm Decision points Branches in the program Branch: point in code where a decision is made and a path selected based on the decision
Identifying Boolean Expressions Goodheart-Willcox Publisher Boolean expressions test conditions to make decisions.
Writing Boolean Expressions Break complex expressions into small steps Use operators to join conditions/outcomes AND operator Expression true if both conditions are true OR operator Expression true if either condition is true
Section 5.2 Review Where are Boolean expressions found in a project’s algorithm? Why should complex expressions be broken down into smaller steps?
Section 5.3 Coding Decisions
Section 5.3 Learning Goal Diagram nested decision-making code statements.
IF…THEN Block Beginning contains the condition to test If the condition is true, Program executes the code contained below within the block If the condition is false, Program will proceed to the next block after the IF…THEN block Use conditional operators when testing multiple conditions Goodheart-Willcox Publisher
IF...THEN…ELSE Block Can be used to offer alternative if condition is false If statement is true, only first block section processed If statement is false, only second block section processed Programming technique for verifying correct user input Goodheart-Willcox Publisher
Creating Nested Decisions Nesting: technique in which one decision is contained within another decision Second decision depends on first decision’s outcome Makes code efficient Processed only until problem is solved Allows for complex response No limit on number of statements nested
Creating Nested Decisions Goodheart-Willcox Publisher Nested statements are created by adding an if then or if then else block into another if then or if then else block.
Checking Calculations Error-checking: programming technique for verifying correct user input Example: user password/security question IF…THEN…ELSE statements used to compare user’s answer to data on file
Section 5.3 Review How can conditional operators be used to test multiple conditions? Describe how nesting is similar to Russian matryoshka dolls. Why is error-checking used in programs?