Decision in Java 1
Page 2: If Statement
The Java if statement is a conditional statement that executes a block of code only if a specified condition is true.
Page 3: If Statement Syntax
Syntax:
if (condition) { //code to be executed if condition is true }
Page 4: Age.java Example with If Statement
Code Example:
public class Age { public void printAge() { byte age = 18; if (age >= 18) { System.out.println("You are an adult."); } } public static void main(String[] args) { Age age = new Age(); age.printAge(); } }
Page 5: Enhanced Age.java Example with User Input
Code Example:
import java.util.Scanner; public class Age { public void printAge() { Scanner sc = new Scanner(System.in); System.out.println("How old are you?"); byte age = sc.nextByte(); if (age >= 18) { System.out.println("You are an adult."); } } public static void main(String[] args) { Age age = new Age(); age.printAge(); } }
Page 6: If Statement Flowchart
Flowchart:
true condition --> statement(s)
false --> rest of code
Page 9: If-Else Statement
The else statement executes a block of code if the condition of an if statement is false.
Page 10: If-Else Statement Syntax
Syntax:
if (condition) { //code to execute if condition is true } else { //code to execute if condition is false }
Page 11: If-Else Statement Flowchart
Flowchart:
START
Is Condition True? --> YES: Statement1
NO: Statement2 --> STOP
Page 12: Enhanced Age.java with If-Else
Code Example:
import java.util.Scanner; public class Age { public void printAge() { Scanner sc = new Scanner(System.in); System.out.println("How old are you?"); byte age = sc.nextByte(); if (age >= 18) { System.out.println("You are an adult."); } else { System.out.println("You are a minor."); } } public static void main(String[] args) { Age age = new Age(); age.printAge(); } }
Page 14: Else-If Ladder Statement
In an else-if ladder, a series of if-else statements are linked together. Each else if has a specific condition.
Page 15: Else-If Statement Syntax
Syntax:
if (condition1) { // Code if condition1 is true } else if (condition2) { // Code if condition2 is true } else if (condition3) { // Code if condition3 is true } else { // Code if none conditions are true }
Page 16: TicketPrice.java Example
Code Example:
import java.util.Scanner; public class TicketPrice { public void determineTicket() { Scanner scanner = new Scanner(System.in); System.out.print("Enter your age: "); int age = scanner.nextInt(); if (age >= 0 && age <= 12) { System.out.println("Ticket Price: Php 100"); } else if (age >= 13 && age <= 17) { System.out.println("Ticket Price: Php 150"); } else if (age >= 18 && age <= 59) { System.out.println("Ticket Price: Php 250"); } else { System.out.println("Ticket Price: Php 150"); } scanner.close(); } public static void main(String[] args) { TicketPrice tp = new TicketPrice(); tp.determineTicket(); } }
Page 18: GradeCalc.java Example
Code Example:
import java.util.Scanner; public class GradeCalc { public void printGrade() { Scanner scanner = new Scanner(System.in); System.out.print("Enter the student's score: "); int score = scanner.nextInt(); if (score >= 95 && score <= 100) { System.out.println("Grade: E"); } else if (score >= 85 && score < 94) { System.out.println("Grade: A"); } else if (score >= 75 && score < 84) { System.out.println("Grade: P"); } else { System.out.println("Grade: NI"); } } public static void main(String[] args) { GradeCalc gc = new GradeCalc(); gc.printGrade(); } }
Page 20: Nested If-Else Statement
Nested if-else statements involve one if-else statement inside another.
Page 21: Nested If-Else Syntax
Syntax:
if (condition1) { if (condition2) { // Code if both conditions are true } else { // Code if condition1 is true, but condition2 is false } } else { // Code if condition1 is false }
Page 22: DriversLicense.java Example
Code Example:
import java.util.Scanner; public class DriversLicense { public void license() { Scanner scanner = new Scanner(System.in); System.out.print("Enter your age: "); int age = scanner.nextInt(); if (age >= 18) { System.out.println("You are an adult."); System.out.print("Do you have a driver's license? (yes/no): "); String hasLicense = scanner.next(); if (hasLicense.equalsIgnoreCase("yes")) { System.out.println("You can drive legally."); } else { System.out.println("You cannot drive legally without a license."); } } else { System.out.println("You are a minor."); } scanner.close(); } public static void main(String[] args) { DriversLicense dl = new DriversLicense(); dl.license(); } }
Page 24: Switch Statement
The switch statement provides a multiway branch, allowing a program to select among several alternatives.
Page 25: Switch Case Statement Syntax
Syntax:
switch (expression) { case value1: // Code if expression equals value1 break; case value2: // Code if expression equals value2 break; // Additional cases as needed default: // Code if no cases match }
Page 27: DayofWeek.java Example
Code Example:
import java.util.Scanner; public class DayOfWeek { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number (1-7): "); int dayNumber = scanner.nextInt(); String day; switch (dayNumber) { case 1: day = "Sunday"; break; case 2: day = "Monday"; break; case 3: day = "Tuesday"; break; case 4: day = "Wednesday"; break; case 5: day = "Thursday"; break; case 6: day = "Friday"; break; case 7: day = "Saturday"; break; default: day = "Invalid day number"; } System.out.println("The day is: " + day); scanner.close(); } }
Page 29: SwitchExample.java Example
Code Example:
public class SwitchExample { public void day() { String day = "Monday"; switch (day) { case "Sunday": System.out.println("It's the first day of the week!"); break; case "Monday": System.out.println("It's the start of the workweek."); break; case "Friday": System.out.println("It's almost the weekend!"); break; default: System.out.println("It's a regular day."); } } public static void main(String[] args) { SwitchExample se = new SwitchExample(); se.day(); } }