If else Java – statement complete tutorial. Here we cover in-depth information with examples on what is if else in java and how it works in programming language.
You can learn basics of if else, where to apply statements in programming. Here learn more about control statements in Java and java tutorials for beginners. Also, don’t forget to check out the Java Interview questions with sample programs and examples. The following article was written by path thomas.
Definition Of If Else Java
If else is a selection statement. It means it can be used to select a block for execution based on a condition. This (if-else) is also known as ” conditional branch statement “. Based on the usage if else in java is classified into different types as follows :
- Simple If Else
- Simple If
- Nested If Else
- If else Ladder
- Nested If .. Etc
Java if else is a ” construction “ used to select one out of two possibilities. With nested if-else construction, we can select one out of multiple possibilities. With a simple if construction, we can choose whether a statement (possibility) should be executed or not.
1.Simple If Else
Syntax for the Simple statement with examples as follows :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
if(condition) { -----; -----; } else { -----; -----; } |
Example for Simple If else in Java: – 1
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class IfSample { public static void main(String a[]) { if(true) System.out.println("condition is true"); if(false) System.out.println("conidtion is false this won't display"); } } |
Output:
1 |
condition is true |
In case if the condition is true, then if block is executed and in case the condition is false, then else block is executed. The condition should always result in a Boolean value. Other types of values are not allowed (in C, other types are also allowed).
There will never be a situation where both parts (if and else) will be executed. Only one part will be executed for sure. Skipping both parts is also not possible.
- if(4<9) is valid
- if(4==9) is valid
- if(4) is not valid
- if( System.out.println(“hello”) ) is not valid
- if( Math.sqrt(90) < 10 ) is valid
- if(Math.pow(2,5) < 10 ) is valid
we can write any number of statements in an if/else block. Those statements should be enclosed within { and }. In case we have only one statement, then the curly braces are optional.
Syntax:
1 2 3 4 5 6 7 |
if(condition) Statement1; else Statement2; |
2.Simple If
- Inside “if”, there should be at least one statement and in “else” also there should be at least one statement.
The following is not valid because there is no statement in “if” part.
1 2 3 4 5 |
if(condition) else statementX; |
- If we don’t want to perform any action in case, the condition is true, then we should use a semi-colon (a dummy statement) as a place holder. So the following is valid:
1 2 3 4 5 |
if(condition); else statementX; |
- similarly if we want to perform some action when the condition is true, and don’t want any action when the condition is false then we can write like the following:
1 2 3 4 5 6 7 |
if(condition) statementA; else ; |
In fact, the “else” part is optional in the if-else construction. So the above code can be written as follows also:
1 2 3 |
If(condition) StatementA; |
- This kind of construction is known as simple if construction.
Multiple Conditions Together:
Sometimes we need to take decisions based on multiple conditions. In that case those conditions can be combined with logical operators like the following:
1 2 3 4 5 6 7 |
if(condition1 || condition2 && condition3 || condition4) Statement1; else Statement2; |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class IfSample { public static void main(String a[]) { int a=77; if( a%10==0 ) System.out.println(a+" is divisble by 10"); else System.out.println(a+" is not divisble by 10"); } } |
1 |
77 is not divisble by 10 |
3.Nested Java If Else Statement:
- Sometimes we need to check “one condition” only when some other condition is satisfied. In that case we can have an java if else construction inside another if/else part(s) like the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
If(condition1) { If(condtion2) { partA; } else { partB; } } else { If(condition3) { partC; } else { partD; } } |
- partA is executed when condition1 is true and condition2 is true.
- partB is executed when condition1 is true and condition2 is false.
- partC is executed when condition1 is false and condition3 is true.
- partD is executed when condition1 is false and condition3 is false.
This kind of if-else construction is known as ” nested if-else construction “. There is no limit on the depth of nesting. With this usage, we can select one option out of multiple possibilities.
Example Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class IfSample { public static void main(String a[]) { int percentage; if(percentage>=75) { if(percentage>=75 && percentage<85) { System.out.println("grade A1"); } else { System.out.println("Grade A"); } } else { if(percentage>=65 && percentage<75) { System.out.println("grade B"); } else if(percentage>=40 && percentage<65) { System.out.println("Grade c"); } else { System.out.println("Fail"); } } } } |
Output:
1 2 3 4 5 6 |
if percentage=90 grade A if percentage=84 grade A1 if percentage=74 grade B if percentage=64 grade C if percentage=42 grade C if percentage=34 fail |
4.Nested If In Java
Sometimes we write “nested if-else” constructions without else parts. Such constructions are generally known as nested-if constructions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
If(condition1) { If(condition2) { If(condition3) { } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class IfSample { public static void main(String a[]) { int percentage; if(true) { if(true) { if(true) { System.out.println("nested if"); } } } } } |
1 |
nested if |
5.If-else ladder
Sometimes we write nested if-else constructions where the nesting takes place in the else parts only. These kinds of constructions are known as if-else ladder (or else-if ladder or if-else-if ladder).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
If(condition1) Statement1; else if(condtion2) Statement2; else if(condition3) Statement3; else if(condition4) Statement4; else Statement5; |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class IfSample { public static void main(String a[]) { int a=1,b=2,c=3,d=4,e=5; if(a>b && a>c && a>d && a>e) { System.out.print(a); } else if(b>c && b>d && b>e) { System.out.print(b); } else if(c>d && c>e) { System.out.print(c); } else if(d>e) { System.out.print(d); } else { System.out.print(e); } System.out.println(" is biggest number of 5 numbers"); } } |
Output:
1 |
5 is biggest of 5 numbers |
6. Series of if-else
We can have any number of if-else constructions in a program. If they are not nested, one if-else has no relation with the other one.
Example Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
class IfSample { public static void main(String a[]) { boolean b; if(b) System.out.println("if 1"); else System.out.println("else 1"); if(b) System.out.println("if 2"); else System.out.println("else 2"); if(b) System.out.println("if 3"); else System.out.println("else 3"); if(b) System.out.println("if 4"); else System.out.println("else 4"); if(b) System.out.println("if 5"); else System.out.println("else 5"); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if b=true output: if 1 if 2 if 3 if 4 if 5 if b=false output: else 1 else 2 else 3 else 4 else 5 |
There should not be any statements between if part and its corresponding else part. So the following code is not valid.
1 2 3 4 5 6 7 8 9 |
If(2<3) a=10; b=20; else c=30; |
here, the statement b=20; will break the association between if and else parts.
Condition should always be placed at if part only. We cannot use a condition at else part. So the following code is not valid.
1 2 3 4 5 6 7 |
if(a<b) x=10; else (b<c) y=20; |