Coding Tutorials in Java
Conditional Statements in Java
Conditional statements are fundamental to controlling the flow of a Java program. They allow you to execute specific blocks of code based on certain conditions, enabling your programs to make decisions and respond dynamically to different inputs.
By the end of this tutorial, you will:
1. The if Statement
Explanation: The if statement allows your program to execute a block of code only if a specified condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
Explanation:
Best Practices:
2. The if-else Statement
Explanation: The if-else statement provides an alternative path of execution when the if condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
Explanation:
Best Practices:
3. The else if Statement
Explanation: The else if statement allows you to check multiple conditions sequentially.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the above conditions are true
}
Example:
int score = 75;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else if (score >= 70) {
System.out.println("Grade: C");
} else if (score >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
Explanation:
Best Practices:
4. The switch Statement
Explanation: The switch statement is a multi-way branch statement that provides an efficient way to dispatch execution to different parts of code based on the value of an expression.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// More cases...
default:
// Code to execute if none of the cases match
}
Example:
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
}
System.out.println(dayName);
Explanation:
Best Practices:
5. The Ternary Operator
The ternary operator in Java is a concise way to perform conditional operations. It acts as a shorthand for an if-else statement and is represented by the ? and : symbols. The general syntax is:
condition ? expression1 : expression2;
The ternary operator evaluates the condition, and based on its result, it returns either expression1 or expression2. It is often used for simple conditional assignments to variables.
Example:
int a = 10, b = 20;
// Using the ternary operator
int max = (a > b) ? a : b;
System.out.println("The maximum value is: " + max);
CopyEdit
In this example, if a > b is true, max will be assigned the value of a. Otherwise, it will be assigned the value of b.
Summary