Posts

Showing posts with the label Java Conditional Statements

Conditional Statements in Java Explained with Syntax

Conditional Statements in Java Conditional statements are used to control the flow of execution in a Java program based on certain conditions. They allow the program to make decisions and execute different blocks of code. Java provides the following conditional statements: if statement if-else statement if-else-if-else statement switch statement Real-Life Examples of Conditional Statements in Java 1. if Statement – Real-Life Example 📌 Example: Checking age for voting Real life: If a person’s age is 18 or more, they are eligible to vote. int age = 20; if (age >= 18) {     System.out.println("Eligible to vote"); } 👉 If the condition is true, the message is printed. 👉 If false, nothing happens. 2. if-else Statement – Real-Life Example 📌 Example: Pass or Fail result Real life: If marks are greater than or equal to 40, the student passes; otherwise, they fail. int marks = 35; if (marks >= 40) {     System.out.println("Pass"); } else {     System.out.pr...