Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.

Presentation on theme: "Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions."— Presentation transcript:

Decision Structures - If / Else If / Else

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.

Decision Structures There are different types of decision structures for us to chose from: If / Else if / Else If / Else if / Else Case Case

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

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 }

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; }

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;

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