Java If-Else Statement
Java is a powerful and versatile programming language that allows for a great deal of complexity and control over your code. One of the critical features of Java, like many other programming languages, is the ability to control the flow of execution through various control flow statements. In this article, we'll focus on one such control flow statement: the 'if-else' statement.
Introduction to 'if-else' Statement in Java
In Java, 'if-else' is a conditional statement that changes the flow of the program based on certain conditions. It is used to decide which block of code should be executed based on whether a particular condition is true or false.
Syntax of 'if-else' Statement
The syntax of the 'if-else' statement in Java is as follows:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Here, the 'condition' is a boolean expression that evaluates to either true or false. If the condition is true, then the code block inside the 'if' branch is executed. If the condition is false, then the code block inside the 'else' branch is executed.
Example of 'if-else' Statement
Let's look at a simple example of using the 'if-else' statement in Java:
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
In this example, the condition checks if the number is greater than 0. Since the number 10 is indeed greater than 0, the code inside the 'if' branch is executed, and "The number is positive." is printed to the console. If the number was not greater than 0, the code inside the 'else' branch would be executed instead.
'if-else' Statement with Multiple Conditions
Java also allows you to use multiple conditions in your 'if-else' statement using logical operators such as '&&' (and), '||' (or), and '!' (not). Here is an example:
int number = 30;
if (number > 0 && number < 50) {
System.out.println("The number is positive and less than 50.");
} else {
System.out.println("The number is either not positive or it is 50 or greater.");
}
In this example, both conditions must be true for the code inside the 'if' branch to be executed. If either of the conditions is false, the code inside the 'else' branch is executed.
The 'else if' Statement
In addition to 'if' and 'else', Java also provides the 'else if' statement, allowing you to check multiple conditions sequentially. Here is an example:
int number = 70;
if (number < 50) {
System.out.println("The number is less than 50.");
} else if (number < 100) {
System.out.println("The number is less than 100 but greater than or equal to 50.");
} else {
System.out.println("The number is 100 or greater.");
}
In this example, the conditions are checked in order. If the first condition is false, the next 'else if' condition is checked, and so on. If none of the 'if' or 'else if' conditions are true, the code inside the 'else' branch is executed.
In summary, 'if-else' statements in Java are a powerful tool for controlling the flow of your program based on certain conditions. They allow your code to make decisions and perform different actions depending on these decisions. Practice using 'if-else' statements in different scenarios to become more comfortable with them and understand their potential.