Download presentation
Presentation is loading. Please wait.
Published byChristian Sutton Modified over 6 years ago
2
Chapter 5 Decisions
3
ssential uestion: How are Boolean expressions or operators used in everyday life?
4
Terms branch condition error-checking nesting selection sequence
5
Making Decisions in Code
Section 5.1 Making Decisions in Code
6
Section 5.1 Learning Goal Compare ways to control how a program runs.
7
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
8
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
9
Making Decisions in Code
Goodheart-Willcox Publisher IF…THEN and IF…THEN…ELSE statements are used to make decisions and reactions in computer programming.
10
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
11
Section 5.1 Review When would selection program control be used over sequence program control? Why is a condition called a Boolean expression?
12
Building Boolean Expressions
Section 5.2 Building Boolean Expressions
13
Section 5.2 Learning Goal Apply operators to Boolean expressions.
14
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
15
Identifying Boolean Expressions
Goodheart-Willcox Publisher Boolean expressions test conditions to make decisions.
16
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
17
Section 5.2 Review Where are Boolean expressions found in a project’s algorithm? Why should complex expressions be broken down into smaller steps?
18
Section 5.3 Coding Decisions
19
Section 5.3 Learning Goal Diagram nested decision-making code statements.
20
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
21
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
22
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
23
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.
24
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
25
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.