Download presentation
Presentation is loading. Please wait.
Published byMelissa Bishop Modified over 11 years ago
1
Decision Structures - If / Else If / Else
2
Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions based on information that we receive. (There is not one solution for all problems!) As such we need a way to have the computer make decisions. As such we need a way to have the computer make decisions.
3
Decision Structures There are different types of decision structures for us to chose from: If / Else if / Else If / Else if / Else Case Case
4
If / Else Structures This structure allows us to make a decision based on the truthfulness of a boolean expression. If the expression is true then the first block of code is executed If the expression is true then the first block of code is executed If the expression is false then the second block will be evaluated If the expression is false then the second block will be evaluated
5
Syntax The syntax for the if /else if / else structure is: if (boolean_expression) { this block evaluates if the expression is true } else if (second boolean expression) { this block evaluates if the second expression is true }else { This block executes if none of the boolean expressions is true }
6
Example if (age <16) {cout << You may not drive; } else if (age > 65) { cout << You must get your eyes checked every year to keep your license; }else { cout << You have no restrictions on your license; }
7
Exceptions If the code in a block has only one command then the curly braces are not required: if (age <16) cout << You may not have a drivers license; else if (age > 65) cout << You must get your eyes checked every year to keep your license; else cout << You have no restrictions on your license;
8
Exercise Create a program that uses the if else structure to make a decision based on the users input. Create a program that uses the if else structure to make a decision based on the users input. Write a program that acts like a multiple choice quiz Write a program that acts like a multiple choice quiz
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.