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
-
if
Statement -
if-else
Statement -
if-else-if
Ladder -
nested if
Statement -
switch
Statement -
ternary (?:)
Operator (alternative to if-else)
1. if
Statement
Syntax:
Example:
2. if-else
Statement
Syntax:
Example:
3. if-else-if
Ladder
Used when there are multiple conditions to check.
Syntax:
Example:
4. nested if
Statement
An if
statement inside another if
statement.
Syntax:
Example:
Java Assignment Programs – Conditional Statements
🔹 Beginner Level
-
Check if a number is positive or negative.
-
Check if a number is even or odd.
-
Find the greatest of two numbers.
-
Find the greatest of three numbers.
-
Check if a person is eligible to vote (age ≥ 18).
-
Check if a year is a leap year.
-
Check if a character is a vowel or consonant.
🔹 Intermediate Level
-
Find the largest among three numbers using
if-else-if
ladder. -
Check whether a number is divisible by 5 and 11.
-
Check if a character is uppercase, lowercase, digit, or special character.
-
Calculate grade based on marks using
if-else-if
. -
Check if a number is positive, negative, or zero.
-
Check whether a number is even or odd using ternary operator.
-
Menu-driven program using
switch
for basic calculator (add, subtract, multiply, divide). -
Find whether a given number is a multiple of 3, 5, or both.
🔹 Advanced Level
-
Accept a 3-digit number and check whether it is an Armstrong number.
-
Accept a character and check if it's an alphabet. If yes, whether it is vowel or consonant.
-
Accept three sides of a triangle and check whether it is valid, and if yes, what type of triangle it is.
-
Write a
switch
case to print the day of the week based on a number (1–7). -
Using nested
if
, check whether a student has passed based on subject-wise marks and overall average.
Comments
Post a Comment