Introduction to if else Java statements
Every programming language has some constructs which are necessary for decision making and also for the flow of control of a program. Decision-making is really important when we get trapped in some dilemma. Java language provides the facility of decision-making with the use of selection statements, which depend on the expression’s truth value. If the condition is true, control goes into a particular block of code, and if it is false, then the control goes to another block of the program.
In this article, we will discuss the if else java statements, which control the flow of programming by providing different conditions. We will learn about nested if else java statements along with solving different examples. Moreover, we will also see how we can use if else java statements in java loops.
In a nutshell, this tutorial is going to have each piece of information that you need to learn about if else java statements.
Getting started with if else Java statements
If else Java statements help to control the flow of the program by providing different conditions and allowing the program to execute the one which is true at a time. The following is the simple diagram of if else java statemetns.
As shown in the diagram above, if the first condition becomes true, the block of codes inside that condition will be executed, and if the condition is false, java will check the next condition without executing the block of code inside the first condition and so on.
if java statement
An if Java statement checks a particular condition; if the condition evaluates to be true, it will execute a statement or a set of statements inside it, otherwise, if the condition is false, it will ignore that statement or set of statements.
if java statement syntax
The simple syntax of java if statements look like this.
//if java statement
if(Condition){
// statements inside if block
}
Example: Using if java statement
Now let us take an example and see how we can use the if statement in the java programming language.
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(true){
System.out.println("the if block is executed!!");
}
}
}
Output:
the if block is executed!!
The test expression must be of the boolean type or the condition inside the if statement should return a boolean type. Now, let take the example of a condition which returns a boolean type. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3>0){
System.out.println("the if block is executed!!");
}
}
}
Output:
the if block is executed!!
Notice that the condition was true that is why if the statement was executed.
if else java statements
The if statement only allows us to execute a set of statements if a condition or expression is true. Suppose we want any other operation to be performed if the condition is false, So, there comes another form of if that allows for such kind of either-or condition by providing an else clause. It is called an if else java statement in which we can use the else statement with an if statement so that we can execute a block of code when the test condition is false.
if else java statement syntax
The simple syntax to use if else java statement looks like this:
// if else java statements
if(condition){
// if statements
}
else{
// else statements
}
Notice that the else statement does not take any condition, because it will be executed when if the statement becomes false. Its execution depends on if statement above it.
Example: Using java if else statement
Now let us take an example and see how the else statement is executed.
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3>0){
System.out.println("the if block is executed!!");
}
else{
System.out.println("the else block is executed!!");
}
}
}
Output:
the if block is executed!!
Notice that the if condition was true that is why the else statement didn't execute. Now let us make the if condition false, and see what happens. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3<0){
System.out.println("the if block is executed!!");
}
// else java condition
else{
System.out.println("the else block is executed!!");
}
}
}
Output:
the else block is executed!!
Notice that this time the else block was executed because the if condition was false.
if .. else .. if java statement
Now let us say we don't have only one condition, and we may have multi-conditions and any one of them can be true at a time. In such cases, one option is to use as many if statements as we want and but that would not be a good idea. Because the if statement is the starting of conditions, and let say our condition becomes true in the very first case, then java will also check for other if statements as well which is not want we want. In such cases, we can use if and else if statements.
if .. else .. if java statement syntax
The simple syntax to use if .. else .. if java statement looks like this:
// if else java statements
if(condition){
// if statemetns
}
else if (condtion2){
// else if statement
}
Notice that the else if statement is also taking condition as an input and will be executed only if that condition becomes true.
Example: Using if .. else .. if java statement
Now let us take an example and see how it actually works.
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3<0){
System.out.println("the if block is executed!!");
}
else if (3>0){
System.out.println("the else if block is executed!!");
}
}
}
Output:
the else if block is executed!!
Notice that the else if statement is executed.
Nested if else Java statements
Nested if else java statements are statements that are inside of another if else java statement. As Java allows nested if statements, we can place an if or else-if statement inside another if statement. In this section, we will have a look at the nested if else java statements along with some simple examples.
Nested if java statement syntax
Nested if java statement is similar to the if statement, with a difference that the outer if statement contains an inner if statement. The simple syntax of nested if java statement looks like this.
// if else java statements
if(condition){
// inner if condition
if(condtion2){
// statements
}
}
Nested if java statement example
Now let us take an example and see how we can use to nested if statements in our java program. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3>0){
if (3<5){
System.out.println("nested if statement is executed!");
}
}
}
}
Output:
nested if statement is executed!
Notice that the inner condition is only executed if the outer condition is true.
Nested if else java statement syntax
Now let us see how the nested if else statement works. The simple syntax will be similar to this one;
// if else java statements
if(condition){
// inner if condition
if(condtion2){
// statements
}
else{
// statements
}
}
Nested if else java statement example
Now let us take a simple example and see how it works. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// if java statement
if(3>0){
if (3>5){
System.out.println("nested if statement is executed!");
}
else{
System.out.println("Nested else statement is executed!");
}
}
}
}
Output:
Nested else statement is executed!
Notice the nested else statement was executed in the above example.
If else Java statements in Java loops
If else statements are important parts of java loops. In while loop if else java statemetns controls the iteration of the loop. In this section we will see how if else java statements play an important role in java loops. Mainly we will discuss java while loop with a condition and the java for loop.
If else java statements and while loop
Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement. If the condition evaluates to true then the blocks of code inside the while loop will be executed unless the condition becomes false. We can use if else java statements in a number of ways in the while loop. One of the ways could be:
// java while loop
while(condition){
if (condition1){
// statements
}
else{
// statements
}
}
Now let us see how we can use the if else java statement to control the flow of the while loop and break when a certain condition is satisfied. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// variable
int num = 0;
while(true){
if (num<10){
System.out.println("counting...."+num);
num++;
}
else{
break;
}
}
}
}
Output:
counting....0
counting....1
counting....2
counting....3
counting....4
counting....5
counting....6
counting....7
counting....8
counting....9
Notice that the condition in the while loop is always true but still we didn't get any infinite loop because we were able to use if else java statements to break the loop when a certain condition was satisfied.
If else java statement and for loop
The “for” loop in Java is an entry-controlled loop that facilitates a user to execute a block of a statement(s) iteratively for a fixed number of times. The number of iterations depends on the test condition given inside the “for” loop. Sometimes we may want to stop our for loop before the initial condition if a certain condition is satisfied. In such case, we can use if else java statement to control. The simplest form could be;
// for loop
for(int i=0; i<10; i++){
if (condition){
// statement
}
else{
// Statements
}
}
Now let us take an example where we want to iterate over 10 numbers and stop the for loop if it gets to number 7. See the example below:
// class
public class Main {
// main method
public static void main(String[] args) {
// for loop
for (int i = 0; i<10; i++){
if (i==7){
break;
}
else{
System.out.println("Printing..."+i);
}
}
}
}
Output:
Printing...0
Printing...1
Printing...2
Printing...3
Printing...4
Printing...5
Printing...6
Notice that our initial condition in for loop was to iterate over 10 numbers but we were able to stop at number 7 using if else java statements.
Summary
The have if statement is used to test the condition. It checks the boolean condition: true
or false
. Similarly, java also supports else and if else statements as well which are checked and executes when the if statement is false. In this tutorial, we learned about if else java statements in more detail. We learned how we can use simple if statements along with else and if else statements by taking various examples. Moreover, we also discussed how we can use if else java statements to control the loops.
In a conclusion, this tutorial provides you solid and deep knowledge of java conditional statements,
Further Reading
If else java statements
conditional statements in java
Switch statements