OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Conditional Statements

Conditional Statements in Java

In Java, conditional statements are used to perform different actions based on different conditions. They control the flow of execution in a program based on whether a condition is true or false.


Types of Conditional Statements in Java

  1. if Statement

  2. if-else Statement

  3. if-else-if Ladder

  4. nested if Statement

  5. switch Statement

  6. ternary (?:) Operator (alternative to if-else)


1. if Statement

Syntax:


if (condition) { // statements to execute if condition is true }

Example:


public class IfDemo { public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println("You are eligible to vote."); } } }

2. if-else Statement

Syntax:


if (condition) { // if block } else { // else block }

Example:


public class IfElseDemo { public static void main(String[] args) { int number = 10; if (number % 2 == 0) { System.out.println("Even number"); } else { System.out.println("Odd number"); } } }

3. if-else-if Ladder

Used when there are multiple conditions to check.

Syntax:


if (condition1) { // statements } else if (condition2) { // statements } else { // default block }

Example:


public class IfElseIfDemo { public static void main(String[] args) { int marks = 75; if (marks >= 90) { System.out.println("Grade A"); } else if (marks >= 75) { System.out.println("Grade B"); } else if (marks >= 60) { System.out.println("Grade C"); } else { System.out.println("Fail"); } } }

4. nested if Statement

An if statement inside another if statement.

Syntax:


if (condition1) { if (condition2) { // nested block } }

Example:


public class NestedIfDemo { public static void main(String[] args) { int age = 25; String nationality = "Indian"; if (nationality.equals("Indian")) { if (age >= 18) { System.out.println("You are eligible to vote in India."); } } } }


Java Assignment Programs – Conditional Statements

🔹 Beginner Level

  1. Check if a number is positive or negative.

  2. Check if a number is even or odd.

  3. Find the greatest of two numbers.

  4. Find the greatest of three numbers.

  5. Check if a person is eligible to vote (age ≥ 18).

  6. Check if a year is a leap year.

  7. Check if a character is a vowel or consonant.


🔹 Intermediate Level

  1. Find the largest among three numbers using if-else-if ladder.

  2. Check whether a number is divisible by 5 and 11.

  3. Check if a character is uppercase, lowercase, digit, or special character.

  4. Calculate grade based on marks using if-else-if.

  5. Check if a number is positive, negative, or zero.

  6. Check whether a number is even or odd using ternary operator.

  7. Menu-driven program using switch for basic calculator (add, subtract, multiply, divide).

  8. Find whether a given number is a multiple of 3, 5, or both.


🔹 Advanced Level

  1. Accept a 3-digit number and check whether it is an Armstrong number.

  2. Accept a character and check if it's an alphabet. If yes, whether it is vowel or consonant.

  3. Accept three sides of a triangle and check whether it is valid, and if yes, what type of triangle it is.

  4. Write a switch case to print the day of the week based on a number (1–7).

  5. Using nested if, check whether a student has passed based on subject-wise marks and overall average.


Comments

Popular posts from this blog

How to Get a Job in Top IT MNCs (TCS, Infosys, Wipro, Google, etc.) – Step-by-Step Guide for B.Tech Final Year Students

Common HR Interview Questions

How to Get an Internship in a MNC