Welcome to The Coding College! In this tutorial, we’ll focus on practical examples of using if...else
statements in Java. This foundational concept in programming helps you make decisions in your code based on conditions.
Syntax Recap
Before diving into examples, here’s a quick recap of the if...else
syntax in Java:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example 1: Check Even or Odd
public class EvenOddExample {
public static void main(String[] args) {
int number = 5;
if (number % 2 == 0) {
System.out.println(number + " is Even");
} else {
System.out.println(number + " is Odd");
}
}
}
Output:
5 is Odd
Example 2: Check Eligibility to Vote
public class VotingEligibility {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Output:
You are not eligible to vote.
Example 3: Find the Largest of Two Numbers
public class LargestNumber {
public static void main(String[] args) {
int num1 = 15, num2 = 25;
if (num1 > num2) {
System.out.println(num1 + " is greater than " + num2);
} else {
System.out.println(num2 + " is greater than " + num1);
}
}
}
Output:
25 is greater than 15
Example 4: Check Pass or Fail
public class PassFail {
public static void main(String[] args) {
int marks = 45;
if (marks >= 40) {
System.out.println("Congratulations! You passed.");
} else {
System.out.println("Sorry, you failed.");
}
}
}
Output:
Congratulations! You passed.
Example 5: Determine Positive or Negative Number
public class PositiveNegative {
public static void main(String[] args) {
int number = -7;
if (number > 0) {
System.out.println(number + " is positive.");
} else {
System.out.println(number + " is negative.");
}
}
}
Output:
-7 is negative.
Example 6: Check Leap Year
public class LeapYear {
public static void main(String[] args) {
int year = 2025;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Output:
2025 is a leap year.
Example 7: Check Temperature
public class TemperatureCheck {
public static void main(String[] args) {
int temperature = 35;
if (temperature > 30) {
System.out.println("It's a hot day!");
} else {
System.out.println("The weather is pleasant.");
}
}
}
Output:
It's a hot day!
Example 8: Check if Number is Zero
public class CheckZero {
public static void main(String[] args) {
int number = 0;
if (number == 0) {
System.out.println("The number is zero.");
} else {
System.out.println("The number is not zero.");
}
}
}
Output:
The number is zero.
Example 9: Determine Grade
public class GradeCalculator {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
Output:
Grade: B
Example 10: Check if Number is Divisible by 5 and 3
public class DivisibilityCheck {
public static void main(String[] args) {
int number = 15;
if (number % 5 == 0 && number % 3 == 0) {
System.out.println(number + " is divisible by both 5 and 3.");
} else {
System.out.println(number + " is not divisible by both 5 and 3.");
}
}
}
Output:
15 is divisible by both 5 and 3.
Practice Problems
- Write a program to check if a number is a single-digit number or not.
- Create a program to find the smallest of two numbers using
if...else
. - Determine if a character is a vowel or a consonant.
Conclusion
The if...else
statement is essential for decision-making in Java. It allows your program to adapt to different conditions and respond accordingly. Practice these examples to strengthen your understanding of conditional statements.
For more tutorials, visit The Coding College and enhance your programming skills! 🚀