Decision in Java
Java Conditional Statements
Overview of Conditional Statements
Conditional statements allow executing different blocks of code based on specified conditions.
IF STATEMENT
Definition
The Java if statement executes a block of code only if a specified condition is true.
Syntax
The basic syntax of an if statement is:
if (condition) { // code to be executed if condition is true }
Example of IF Statement
Age.java 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(); } }
Flowchart Representation
Flowchart illustrates the flow of control based on the condition being true or false:
If condition is true: Execute statements
If condition is false: Proceed to other code
IF-ELSE STATEMENT
Definition
The else statement in Java executes a block of code if the condition of an if statement is false.
Syntax
The basic syntax:
if (condition) { // code if condition is true } else { // code if condition is false }
Example of IF-ELSE Statement
Age.java Example with else:
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(); } }
Flowchart Representation
Flowchart illustrates decision flow based on true or false conditions:
Start
Test condition
Execute appropriate statement based on the result
ELSE-IF STATEMENT
Definition
The else-if ladder permits multiple conditions to be checked in sequence. If one condition is true, its block executes, skipping others.
Syntax
The basic syntax for an else-if sequence is:
if (condition1) { // code if condition1 is true } else if (condition2) { // code if condition2 is true } else { // code if none of the conditions are true }
Example of ELSE-IF Statement
TicketPrice.java 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(); } }
Flowchart Representation
Flowchart represents flow of evaluation for each condition which leads to respective code execution.
NESTED IF-ELSE STATEMENT
Definition
Nested if-else statements occur when one if-else is contained within another, allowing for more complex decision-making.
Syntax
The basic syntax:
if (condition1) { if (condition2) { // code if both conditions are true } else { // code if condition1 is true, condition2 is false } } else { // code if condition1 is false }
Example of NESTED IF-ELSE Statement
DriversLicense.java 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(); } }
Flowchart Representation
Flowchart describes the decision-making path that includes nested conditions.