LESSON 14
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define conditionals in Java programming.
- Use if statements in Java applications.
- Use if-else statements for decision making.
- Use nested if statements in Java applications.
- Use switch statements to control program flow.
3.2 Overview
Java applications frequently need to make decisions during execution. Conditionals allow programs to execute specific instructions depending on whether conditions evaluate to true or false.
This lesson introduces learners to conditional statements in Java and explains how conditionals are used to control application logic and decision making. Learners will also explore different conditional structures used in software development.
Conditionals are commonly used in:
- banking systems,
- login systems,
- inventory systems,
- ATM applications,
- and enterprise software.
Understanding conditionals is essential because they control the logical flow of Java applications.
KT1401 — Introduction to Conditionals
Conditionals are programming structures used to make decisions in Java applications.
Conditionals evaluate conditions and determine which block of code should execute.
Conditions normally return:
- true,
- or false.
Importance of Conditionals
Conditionals help developers:
- control application behaviour,
- validate data,
- perform decision making,
- and automate logical processing.
KT1402 — The if Statement
The if statement executes code only when a condition is true.
Syntax
if(condition) {
// code
}
Example
public class Main {
public static void main(String[] args) {
int marks = 75;
if(marks >= 50) {
System.out.println("Pass");
}
}
}
Output
Pass
Explanation
The condition:
marks >= 50
evaluates to:
true
Therefore, the code inside the if block executes.
Importance of the if Statement
The if statement is commonly used for:
- validation,
- authentication,
- and decision-making processes.
KT1403 — The if-else Statement
The if-else statement provides two possible execution paths.
If the condition is true:
- the if block executes.
If the condition is false:
- the else block executes.
Example
public class Main {
public static void main(String[] args) {
int age = 16;
if(age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
}
}
Output
Minor
Importance of if-else Statements
if-else statements help applications:
- process alternative outcomes,
- manage logical branching,
- and respond dynamically to conditions.
KT1404 — Nested if Statements
A nested if statement is an if statement placed inside another if statement.
Nested if statements allow multiple conditions to be evaluated.
Example
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasID = true;
if(age >= 18) {
if(hasID) {
System.out.println("Access Granted");
}
}
}
}
Output
Access Granted
Importance of Nested if Statements
Nested if statements are used when:
- multiple conditions must be checked,
- complex validation is required,
- and applications require layered decision making.
KT1405 — The switch Statement
The switch statement selects one option from multiple possible cases.
The switch statement improves readability when handling multiple choices.
Syntax
switch(variable) {
case value:
// code
break;
default:
// code
}
Example
public class Main {
public static void main(String[] args) {
int option = 2;
switch(option) {
case 1:
System.out.println("Home");
break;
case 2:
System.out.println("Profile");
break;
default:
System.out.println("Invalid Option");
}
}
}
Output
Profile
Importance of switch Statements
switch statements are commonly used in:
- menu systems,
- ATM systems,
- navigation systems,
- and option-based applications.
KT1406 — Comparison Operators in Conditionals
Conditional statements commonly use comparison operators.
Comparison Operators
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example
public class Main {
public static void main(String[] args) {
int number = 10;
if(number == 10) {
System.out.println("Correct");
}
}
}
Output
Correct
Importance of Comparison Operators
Comparison operators support:
- validation,
- calculations,
- conditional processing,
- and application logic.
KT1407 — Real-World Use of Conditionals
Conditionals are used in:
- login systems,
- ATM systems,
- inventory systems,
- healthcare applications,
- and enterprise software.
Examples:
- validating passwords,
- checking account balances,
- determining user permissions,
- and processing application rules.
Conditionals control the logical behaviour of software systems.
3.5 Key Notes / Summary
- Conditionals allow Java programs to make decisions.
- if statements execute code when conditions are true.
- if-else statements support alternative execution paths.
- Nested if statements evaluate multiple conditions.
- switch statements handle multiple options.
- Comparison operators evaluate conditions.
- Conditionals control application logic and program flow.