Lesson 4: Decision Control Structures
Lesson Overview & Midterm Context
- Instructor: JHON RENZ BRYAN DELA FUENTE
- Lesson Title: Lesson 4. Decision Control Structures
- Midterm Examination Notice: Lesson 4 is explicitly included in the Midterm Examination. Due to class cancellation, this lesson is assigned for self-study using the designated web resources.
- Core Topics:
- Topic 1: Conditional Structures
- Topic 2: Decision-making in program design
- Assigned Reference Links:
https://www.w3schools.com/java/java_conditions.asphttps://www.w3schools.com/java/java_conditions_else.asphttps://www.w3schools.com/java/java_conditions_elseif.asphttps://www.w3schools.com/java/java_conditions_shorthand.asphttps://www.w3schools.com/java/java_conditions_nested.asphttps://www.w3schools.com/java/java_conditions_logical.asphttps://www.w3schools.com/java/java_switch.asp
Topic 1: Conditional Structures & Java If Statements
- Mathematical Logical Operators: Java conditions leverage standard mathematical comparisons that return a boolean value (
true or false):- Less than:
a < b - Less than or equal to:
a <= b - Greater than:
a > b - Greater than or equal to:
a >= b - Equal to:
a == b - Not Equal to:
a != b
- The
if Statement:- Used to specify a block of Java code to be executed only if a given condition evaluates to
true. - Syntax:
java
if (condition) {
// block of code to be executed if the condition is true
}
- Syntax Casing Rule: Java keywords are strictly case-sensitive. The keyword
if must be written in lowercase; using IF or If generates a compiler error.
The else and else if Branching Statements
- The
else Statement:- Used to specify a alternative block of code to be executed if the
if condition evaluates to false. - Syntax:
java
if (condition) {
// block of code to be executed if condition is true
} else {
// block of code to be executed if condition is false
}
- The
else if Statement:- Used to specify a new condition to test if the preceding condition evaluates to
false. - Syntax:
java
if (condition1) {
// block executed if condition1 is true
} else if (condition2) {
// block executed if condition1 is false and condition2 is true
} else {
// block executed if both condition1 and condition2 are false
}
- Sequential Execution: The program tests conditions in top-down order. The moment a condition evaluates to
true, its corresponding code block is executed and all subsequent branches in the block are bypassed.
Short Hand If…Else (Ternary Operator)
- Definition: The ternary operator provides a concise, single-line mechanism for conditional evaluation and variable assignment.
- Syntax Structure:
variable = (condition) ? expressionTrue : expressionFalse;
- Component Breakdown:
condition: A boolean expression tested by the operator.?: Symbol separating the tested condition from the true outcome expression.expressionTrue: Evaluated and returned if the condition is true.:: Symbol separating the true expression from the false outcome expression.expressionFalse: Evaluated and returned if the condition is false.
Topic 2: Decision-Making in Program Design
- Nested Conditional Structures:
- Involves placing an
if or else if statement inside another if or else block. - Used when dependent decisions require a primary pre-requisite condition to evaluate to
true before checking subordinate conditions. - Syntax:
java
if (condition1) {
// Executes if condition1 is true
if (condition2) {
// Executes only if both condition1 and condition2 are true
}
}
- Logical Operators in Decision Control:
- Used to combine multiple boolean conditions or invert logical states within conditional control statements.
- Logical AND (
&&): Returns true only if both conditions/statements evaluate to true. - Logical OR (
||): Returns true if at least one of the conditions/statements evaluates to true. - Logical NOT (
!): Inverts the logical result; returns false if the statement is true, and true if it is false.
Multi-Way Decision Structures: The Java Switch Statement
- Purpose: Used to select one of many distinct code blocks to execute based on exact matching of an expression value, serving as a structured alternative to multi-branch
if...else if chains. - Syntax Structure:
java
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
- Key Control Keywords:
switch: Evaluates an expression once and compares its resulting value against each case label.case: Defines a candidate target value to match against the switch expression.break: Halts execution and exits the switch block upon execution. Without a break statement, execution falls through into subsequent case blocks regardless of whether their condition matches.default: Optional clause executed if none of the defined case values match the evaluated expression.