Conditional Statements in Java
Conditional statements allow your code to make decisions based on certain conditions. The most common conditional statements in Java are if
, if-else
, and switch
.
Akali vestundi, if(Opika unte) cook maggi else { paduko } , oka decision thiskovadaniki manam e conditional statements ni use chestam.
Code examples clear ga telugu lo untai, try to understand.
1. if
Statement
The if
statement evaluates a condition. If the condition is true, the code block within the if
statement is executed.
int attendence = 75;
if (attendece >= 75) {
System.out.println("Intlo hayiga padukundam");
}
2. if-else
Statement
The if-else
statement provides an alternative action if the condition is false.
int attendence = 70;
if (attendece >= 75) {
System.out.println("Intlo hayiga padukundam");
} else {
System.out.println("Thakuva aithe kastam!");
}
3. if-else-if
Ladder
The if-else-if
ladder allows multiple conditions to be evaluated in sequence.
int attendence = 40;
if (attendence >= 75) {
System.out.println("Intlo hayiga padukundam");
} else if (attendence >= 50) {
System.out.println("Thakuva aithe kastam!");
} else {
System.out.println("Nanna, nuvu intiki rara ni pani cheptha..");
}
4. switch
Statement
The switch
statement allows you to choose between multiple options based on the value of a variable.
mari akuva else if raste pedhaga aipothadi kadha, dini alternative ga use chestham
int day = 3;
switch (day) {
case 1:
System.out.println("Sunday, Movie chudham");
break;
case 2:
System.out.println("Monday, College povali");
break;
case 3:
System.out.println("Tuesday, Jai Hanuman");
break;
default:
System.out.println("Invalid day, naku ardam kaledhu sarr..");
break;
}
Conditional (Ternary) Operator
The conditional (ternary) operator is a shorthand way of writing an if-else
statement.
if else ni oka line lo rayadaniki shortcut.
indulo (= , ? , : )e 3 operators use chestham kabati ternary antaru.
variable = (condition) ? True_value : False_value;
Example:
int number = 10;
String result = (number > 0) ? "Positive" : "Negative";
System.out.println(result);
Problems to Solve
Problem 1: Check Leap Year
Write a Java program to check whether a year is a leap year or not.
Hint: A leap year is divisible by 4 but not by 100 unless it is also divisible by 400.
Problem 2: Find the Largest Number
- Write a Java program to find the largest of three numbers using conditional statements.
Problem 3: Simple Calculator Using
switch
- Write a Java program to create a simple calculator that can perform addition, subtraction, multiplication, and division based on user input using a
switch
statement.
- Write a Java program to create a simple calculator that can perform addition, subtraction, multiplication, and division based on user input using a
Problem 4: Determine the Grade
Write a Java program to determine the grade of a student based on their score. Use the following scale:
90-100: A
80-89: B
70-79: C
60-69: D
0-59: F
Problem 5: Even or Odd
- Write a Java program to check whether a given number is even or odd using the conditional (ternary) operator.
Subscribe to my newsletter
Read articles from Kandadi Manasa directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by