Decision Structures
Learning Objectives
In addition to all previous competencies, upon completion of this unit, you should be able to:
- Make single decisions with the simple if statement
- Make dual alternative decisions with the simple if/else statement
- Make multiple alternative decisions with the if/ else if/ else statement
- Make decisions with the switch structure
- Use logical operators to create multiple boolean expressions
- Perform validation on end-user input using Scanner methods such as
.hasNextInt()and.hasNextDouble() - Understand and code nested decision structures
Unit Learning Activities
Decision Structures are a fundamental concept in computer programming. They are sometimes called control statements. When you use a decision structure, you control the flow of your application. It is up to you to “decide” what to do based on the desired end results. Using decision structures is like flow-charting your code. If thing “A” happens, then do thing “B”, however, if thing “A” does not happen, then do thing “C”.
Decision structures are instrumental for end-user validation. You have seen this in action in my example code. Also, think about it...if a required field on a form is blank, you do not want to proceed with processing. If “blank” ask end-user to complete the form, else continue processing.
Each link below takes you to a specific chapter within this unit. Please take the time to review all the chapters and when available, complete the interactive activities.
"I think and think for months and years. Ninety-nine times, the conclusion is false. The hundredth time I am right." - Albert Einstein
- Single Decision Structure
- What is “true condition”?
- Relational Operators
- Equality (
==) - Inequality (
!=) - Greater Than (
>) - Less Than (
<) - Greater Than or Equal (
>=) - Less Than or Equal (
<=) - Decision Structure Syntax
- Detailed Example
- Dual Alternative Decision
Structure
- Boolean Expression
- Difference Between
ifandif/else - Test Your Knowledge - Interactive activity
- Detailed Example
- Multiple Alternative Decision
Structure
- Testing One Variable for Different Values
- The Flow of the Code
- Interactive Live Demo
- Limit Your
else ifStatements - Detailed Example
- Multiple Boolean Expressions
- The Logical OR (
||) Operator - The Logical AND (
&&) Operator - Common Errors
- Detailed Example
- The Logical OR (
- Nested Decision Structures
- Nested
ifStatements - Nested
if/elseStatements - Interactive Live Demo
- Nested
- The
switchStructure- The
switchStatement - Take a
break; - Things to Remember
- Detailed Example
- The
- This unit includes these full-process examples
- Sort three numbers using the Single Decision Structure
- Divide Safely using the Dual Alternative Decision Structure
- Process Customer Record using the Multiple Alternative Decision Structure
- Get a Driver's License using Multiple Boolean Expressions
- Process Customer Record using the
switchStructure