Day 4
Conditional Statements
Java Conditional Statements
Java conditional statements allow you to control the flow of your program by executing certain
pieces of code based on specific conditions.
1) if Statement
The if statement is the most basic form of conditional statement. It checks a condition and executes
the block of code inside it if the condition is true.
Syntax:
Example:
2) if..else Statement
The if..else statement provides an alternative block of code to execute if the condition in the if
statement is false.
Syntax:
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Example:
3) Nested if Statement
A nested if statement is an if or if..else statement inside another if or if..else statement. It allows you
to check multiple conditions sequentially.
Syntax:
Example:
4) switch Statement
The switch statement is used to execute one block of code out of many based on the value of an
expression. It's an alternative to using multiple if..else if statements.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Syntax:
Example:
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
Lab Assignments
if Statement
1. Write a program that takes an integer input and checks if the number is positive.
2. Write a program that takes an age as input and checks if the person is eligible to vote.
3. Write a program that takes a year as input and checks if it is a leap year.
4. Write a program that takes a character as input and checks if it is an uppercase letter.
5. Write a program that takes an integer input and checks if the number is a multiple of 10.
if..else Statement
1. Write a program that takes an integer input and determines whether it is even or odd.
2. Write a program that takes an age as input and checks if the person is a teenager (age
between 13 and 19).
3. Write a program that takes two integers as input and prints the larger number.
4. Write a program that takes an integer input and checks if the number is positive, negative, or
zero.
5. Write a program that takes an age as input and checks if the person is eligible for a senior
citizen discount (age >= 60).
Nested if Statement
1. Write a program that takes an integer input and checks if it is both positive and even.
2. Write a program that takes a character as input and checks if it is an uppercase vowel.
3. Write a program that takes three integers as input and finds the largest number.
4. Write a program that takes an integer input and checks if it is a multiple of both 5 and 10.
5. Write a program that takes a score as input and determines the grade based on the score
(e.g., A, B, C, etc.).
6. Write a program that takes a character as input and checks if it is a vowel or consonant.
7. Write a program that takes an integer input and checks if it is divisible by both 2 and 3.
switch Statement
1. Write a program that takes an integer (1-7) as input and prints the corresponding day of the
week.
2. Write a program that takes an integer (1-12) as input and prints the corresponding month
name.
3. Write a program that takes a score as input and displays the corresponding grade using a
switch statement.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan
4. Write a program that takes two numbers and an operator (+, -, *, /) as input and performs
the corresponding arithmetic operation.
5. Write a program that takes an integer (1-12) as input and prints the corresponding season.
https://www.pavanonlinetrainings.com https://www.youtube.com/@sdetpavan