Within programming, decision-making is a crucial aspect that influences the sequence of actions. Whether it involves managing traffic at a busy junction or handling different scenarios within a program, the ability to make decisions is vital. In Java programming, the 'if-else' statement stands as a fundamental tool for decision-making. Let's explore the concept of 'if-else' statements, understand their functionality, and discover how they can be efficiently applied in Java programming.
The Java if statement is used to test the condition. It checks the Boolean condition: true or false. There are mainly four types of if statements in Java.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
Java if Statement
The most basic form of control structure used for decision-making in Java is the if statement. It runs the code within the if block only if the specified condition evaluates to true. By assessing a Boolean condition, it triggers the block of code contained within the if statement when the condition holds true. In cases where the condition is false, this statement simply skips the block. Employing specific criteria aids in managing the flow of the program. The if statement is commonly utilized in practical scenarios for conditional executions, verifications, and validations.
Syntax:
It has the following syntax:
if(condition){
//code to be executed
}
Example: Checking Age
In this example, we will illustrate how to determine a person's age by using the if statement in Java programming.
Example
//Java Program to demonstate the use of if statement.
public class Main {
public static void main(String[] args) {
//defining an 'age' variable
int age=20;
//checking the age
if(age>18){
System.out.print("Age is greater than 18");
}
}
}
Output:
Age is greater than 18
Java if-else Statement
The if-else statement in Java serves as a fundamental control mechanism that permits the execution of various code blocks based on the evaluation of a condition. If the condition is true, the if block is executed; otherwise, the else block is triggered. This feature facilitates making decisions in a program and allows for the modification of the execution flow depending on varying conditions. It is commonly employed in scenarios such as error management, conditional branching, and data validation in real-world applications.
Syntax:
It has the following syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
Example: Checking EVEN or ODD
Let's consider a Java program that determines whether a number is odd or even using the if-else statement.
Example
public class Main {
public static void main(String[] args) {
//defining a variable
int number=13;
//Check if the number is divisible by 2 or not
if(number%2==0){
System.out.println("even number");
}else{
System.out.println("odd number");
}
}
}
Output:
odd number
Example: Checking Leap Year
A year qualifies as a leap year if it can be evenly divided by both 4 and 400, but not by 100.
Example
public class Main {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println("LEAP YEAR");
}
else{
System.out.println("COMMON YEAR");
}
}
}
Output:
LEAP YEAR
Java if-else-if ladder Statement
The if-else-if ladder is used to select and execute one statement out of multiple conditions. It consists of an if condition followed by one or more else if conditions, with an optional else block at the end. When the program is executed, each condition is evaluated sequentially from top to bottom, and the block associated with the first true condition is executed. If none of the conditions are met, the else block is executed. This statement provides a clear and concise way to manage multiple possible paths in a program.
Syntax:
It has the following syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Example: Checking Grading System
To illustrate the process of verifying a grading system using an if-else-if ladder statement in Java, we will consider an example.
Example
//Java Program to demonstrate the use of If else-if ladder.
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.
public class Main {
public static void main(String[] args) {
int marks=65;
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}else if(marks>=90 && marks<100){
System.out.println("A+ grade");
}else{
System.out.println("Invalid!");
}
}
}
Output:
C grade
Example: Checking Positive, Negative, or Zero
Consider an illustration to showcase the procedure of verifying positive, negative, and zero values utilizing the if-else if ladder statement in the Java programming language.
Example
public class Main {
public static void main(String[] args) {
int number=-13;
if(number>0){
System.out.println("POSITIVE");
}else if(number<0){
System.out.println("NEGATIVE");
}else{
System.out.println("ZERO");
}
}
}
Output:
NEGATIVE
Java Nested if statement
In Java, the concept of including one if statement within another is referred to as a "nested if statement." This technique establishes a hierarchical system for decision-making, enabling the examination of a secondary condition solely if the initial condition is met. It proves beneficial for evaluating a series of interconnected conditions. While nested if statements are potent, it is advisable to employ them judiciously to avoid complicating the code's readability and maintainability.
Syntax:
It has the following syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
Example: Checking Elegibility for Blood Donation
Example
//Java Program to demonstrate the use of Nested If Statement.
public class Main {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=20;
int weight=80;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
}
}
}}
Output:
You are eligible to donate blood
Example: Checking Elegibility for Blood Donation
Example
//Java Program to demonstrate the use of Nested If Statement.
public class Main {
public static void main(String[] args) {
//Creating two variables for age and weight
int age=25;
int weight=48;
//applying condition on age and weight
if(age>=18){
if(weight>50){
System.out.println("You are eligible to donate blood");
} else{
System.out.println("You are not eligible to donate blood");
}
} else{
System.out.println("Age must be greater than 18");
}
}
}
Output:
You are not eligible to donate blood
Ternary Operator
The ternary operator (? :) can be utilized as an alternative to an if...else statement, offering a more concise method to evaluate a condition. When the condition is true, the value following ? is produced, whereas when the condition is false, the value following : is returned.
Example
public class Main {
public static void main(String[] args) {
int number=13;
//Using ternary operator
String output=(number%2==0)?"even number":"odd number";
System.out.println(output);
}
}
Output:
odd number