Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Conditional statements, like the if-else statement in Java, control program flow based on conditions. The if-else syntax consists of an initial condition followed by code blocks for true and false outcomes. It enables executing different code paths depending on whether a condition evaluates to true or false.
In this blog, we will discuss Conditional Statements in Java (If-Else Statement) in detail. Let's start going!
What are Conditional Statements in Java?
Conditional statements in Java are utilized to make decisions in a program based on some specific conditions. The main constraints are ‘if,’ ‘else,’ and ‘else if.' These statements allow the program to execute different lines of code depending on the scenario, whether a particular condition is true or false. These are vital for controlling the flow of a program that enables the developers to create the logic that is able to respond dynamically to the data or the input, which further helps in making the program more powerful and flexible.
Types of Java Conditional Statements
There are five types of Java conditional statements. All of these are widely used according to their syntaxes and use cases. We will discuss them further in detail so let us note them down below:
1. Java If Statement
2. Java If-Else Statement
3. Java If-Else-If Ladder Statement
4. Java Nested If Statement
5. Java Switch Statement
Java If Statement
Suppose a condition is true if a statement is used to run the program. It is also known as a one-way selection statement. If a condition is used, an argument is passed, and if it is satisfied, the corresponding code is executed; otherwise, nothing happens.
Syntax
The syntax of the If statement is:-
if (expression) {
// You can enter the code here
}
Working of Java If Statement
If conditional statements in java works on the following given statement:-
1. Statements in the block are executed if the expression evaluates to nonzero.
2. Control is transferred to the statement that follows the expression if it evaluates to zero (false).
Example
The below given example helps in understanding the if statement in Java.
Java
Java
import java.util.*; class sample { public static void main(String[] args) { int a = -3; if (a < 0) { System.out.println(a + " is a Negative Number."); } } }
You can also try this code with Online Java Compiler
An if-else statement is a control structure that determines which statements to choose based on a set of conditions.
Syntax
The syntax of the If-Else conditional statements in java is:-
if (condition) {
// Statements that will be carried out if the condition is satisfied
} else {
// Statements that will be carried out if the condition is not met
}
Working of Java If-Else Statement
The statements in the if block is carried out if the condition is met. Otherwise, the statements in the else block are carried out. Always follow an if clause with an else clause. If not, the compiler will produce a "misplaced else" error.
Example
The example below helps us understand the if-else conditional statements in Java.
Java
Java
import java.util.*; class sample { public static void main(String[] args) { int a = 10; if (a % 2 == 0) { System.out.println(a + " is an even number"); } else { System.out.println(a + " is an odd number"); } } }
You can also try this code with Online Java Compiler
The Java ternary operator offers a concise syntax for determining if a condition is true or false. It returns a value based on the outcome of the Boolean test.
One can replace their Java if-else statement using a ternary operator to create compact code. Expert coders enjoy the clarity and simplicity the Java ternary operator adds to their code.
The syntax of the Java Ternary operator is as follows:
(condition) ? (return if true) : (return if false);
Let's understand the concept with the help of a code example. We will see the above code using the ternary operator instead of the Java If-else statement.
Java
Java
class example { public static void main( String args[] ) { int a = 10; String teropt = (a % 2 == 0) ? a + " is an even number" : a + " is an odd number"; System.out.println(teropt); } }
You can also try this code with Online Java Compiler
An if-else-if ladder in Java can execute one code block while multiple other blocks are executed.
Syntax
The syntax of the If-Else-If conditional statements in java is:-
if (condition1) {
// Executable code if condition1 is true,
} else if (condition2) {
// Executable code if condition2 is true
}
...
else {
// Executable code if all the conditions are false
}
Working of Java If-Else-If Ladder Statement
The conditional expressions are evaluated in the If-Else-If statement from top to bottom. The statement associated with a true condition is executed as soon as it is found, and the rest of the ladder is skipped. The final else statement is executed if all the conditions are false.
Example
The below given example helps in understanding the if-else-if ladder conditional statements in Java.
Java
Java
import java.util.*; class sample { public static void main(String[] args) { int i = 3; if (i == 1) System.out.println("January"); else if (i == 2) System.out.println("February"); else if (i == 3) System.out.println("March"); else System.out.println("April"); } }
You can also try this code with Online Java Compiler
If conditions inside another if conditions are called as Nested If conditional statements in java.
Syntax
The syntax of the nested if conditional statements in java are:-
if (condition1) {
// Statement 1 will execute
if (condition2) {
// Statement 2 will execute
}
}
Working of Java Nested If Statement
The way nested if statements operate assumes that they must first become true and sufficient for the other states with the second condition for them to be used, even though other statements may choose to proceed with a false condition if the first condition is met.
Example
The example below helps us understand the Nested If conditional statements in Java.
Java
Java
System.out.println("i is smaller than 15 too"); else System.out.println("i is greater than 20"); } } }
You can also try this code with Online Java Compiler
Unlike the if-else statement, the switch statement has more than one way to be executed. Additionally, it compares the expression's value to its cases by focusing its evaluation on a few primitive or class types.
Syntax
The syntax of the Switch statement is:-
switch (Expression) {
case value 1:
// Statement 1;
case value 2:
// Statement 2;
case value 3:
// Statement 3;
...
case value n:
// Statement n;
Default:
// default statement;
}
Working of Java Switch Statement
Java's switch case allows multiple conditions to be checked simultaneously, similar to an if-else ladder. A constant or a literal expression that can be evaluated is given to Switch.
Each test case's value is compared to the expression's value until a match is made. The associated code is executed if the specified default keyword does not find a match. Otherwise, the code designated for the test case that matches it is run.
Example
Java
Java
import java.util.*; class Sample { public static void main(String[] args) { int mon = 2; switch (mon) { case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
default: System.out.println("Not present in first half month of year"); } } }
You can also try this code with Online Java Compiler
Best for checking multiple conditions that can't be represented by a single expression
Best for checking a single expression against multiple values
Evaluates a boolean expression to determine which branch to execute
Evaluates an expression to determine which case to execute
Can handle complex conditions with multiple boolean expressions
Can handle a limited set of values that can be matched
Can use all relational operators
Can only use the == operator to compare values
Frequently Asked Questions
What are the conditional statements in Java?
Conditional statements in Java are used to make decisions based on certain conditions. The most common conditional statements in Java are the If-Else statement, the Switch statement, and the Ternary Operator. These statements allow the program to execute different blocks of code based on specific conditions.
What are the 4 conditional statements in Java?
The four conditional statements in Java are: if, if-else, else-if ladder, and switch. The 'if-else' statement executes different code based on whether the condition is true or false. The 'switch' statement evaluates an expression and executes the code corresponding to the matching case.
What are conditional statements in OOP?
In OOPs, conditional statements are used to tell the computer what action to take in response to certain conditions. The execution flow may alter if a condition is added to a block of statements depending on the result of the condition's evaluation.
Conclusion
We have discussed the Conditional Statements in Java (If-Else Statement). We further discussed the If, If-Else, and switch conditional statements in Java.
We hope this blog has helped you enhance your knowledge of Conditional Statements in Java. Do not stop learning! We recommend you read some of our Java articles:-