Decision in Java
Java Conditional Statements Notes
Page 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: Example - Age.java
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: Extended Example - Age.java with Scanner
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 Representation:
True condition: Executes statement(s).
False condition: Proceeds to the rest of the code.
Page 8: F-ELSE STATEMENT JAVA
Page 9: IF-ELSE STATEMENT
The else statement in Java 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 be executed if condition is true } else { // code to be executed if condition is false }
Page 11: IF-ELSE STATEMENT FLOWCHART
Flowchart Representation:
START
Is Condition True?
Yes: Executes Statement 1
No: Executes Statement 2
STOP
Page 12: Extended Example - 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 13: DECISIONS ELSE-IF JAVA LADDER
Page 14: ELSE-IF LADDER STATEMENT
In an "else-if ladder," a series of if-else statements are linked together. Each else-if is associated with a specific condition evaluated sequentially.
Page 15: IF-ELSE STATEMENT SYNTAX
Syntax for Else-If:
if (condition1) { // Code for condition1 true } else if (condition2) { // Code for condition2 true } else if (condition3) { // Code for condition3 true } else { // Code if none are true }
Page 16: Extended Example - TicketPrice
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 17: ELSE-IF STATEMENT FLOWCHART
Flowchart Representation:
Test Expression 1: If true, execute Statement 1.
Test Expression 2: If true, execute Statement 2.
Test Expression 3: If true, execute Statement 3.
Body of else: Contains the following if-else statement.
Page 18: Extended Example - GradeCalc
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 19: DECISIONS NESTED JAVA IF-ELSE
Page 20: NESTED IF-ELSE STATEMENT
Nested if-else statements refer to the situation where one if-else statement is nested inside another.
Page 21: NESTED IF-ELSE STATEMENT SYNTAX
Syntax:
if (condition1) { // Code if condition1 true if (condition2) { // Code if both true } else { // Code if condition1 true, but condition2 false } } else { // Code if condition1 false }
Page 22: Extended Example - DriversLicense
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 23: NESTED IF-ELSE FLOWCHART
Flowchart Representation:
True condition: Execute Body of nested If.
False condition: Proceed to else condition.
Page 24: DECISIONS SWITCH JAVA CASE
Page 25: SWITCH CASE STATEMENT
The switch statement enables a program to select among several alternatives, providing efficiency for multiway branching as compared to nested if statements.
Page 26: SWITCH CASE STATEMENT SYNTAX
Syntax:
switch (expression) { case value1: // Code for value1 break; case value2: // Code for value2 break; // Additional cases as needed default: // Code if expression doesn't match any case }
Page 27: Extended Example - DayofWeek
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 28: SWITCH CASE FLOW CHART
Flowchart Representation:
Expression Matches: Checks each case one by one.
Default case: If no matches are found, executes the default code.
Page 29: Extended Example - SwitchExample
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(); } }