Conditional Statements In JAVA
In programming Conditional statements use when we have to decide one thing among two or more than two statements like if we have to decide either we have to add two no or multiply them then to solve that issue we use conditional statements .
Four most popular types of conditional statements in java are:
- IF statement
- IF else Statement
- Dangling Else Problem
- Switch Statement
- Ternary Operator
1. If Statement:
If statement check only one condition and if the condition is true then the body is executed otherwise nothing will happen.
Syntax:
if(condition){
//body….
}
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package myfirstproject; import java.util.*; public class MyFirstProject { public static void main(String args[]) { int a=5; if(a==5){ System.out.println("Yes the condition is true"); } } } |
Output:
1 |
Yes the condition is true |
If we put a=6 and then check the condition the result is nothing.
2. IF Else Statement:
If else statement also has two types with one else and with multiple else in single else if the first if condition is wrong then else execute and if, If execute then it will skip the else part.
Syntax:
if(condition){
//body
} else {
//body
}
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 |
package myfirstproject; import java.util.*; public class MyFirstProject { public static void main(String args[]) { int a=6; if(a==5){ System.out.println("Yes the condition is true"); } else { System.out.println("Your Condtion is false"); } } } |
Output:
1 |
Your Condition is false |
3. IF Else If Statement:
It have multiple else statement if, first if condition is false then check first its else if it’s also wrong then next and if all else if statements are wrong then execute last else statement.
Syntax:
if(condition){
//body here
} else if(condition){
//body here
}else if(condition){
//body here
}else{
//body here
}
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 |
package myfirstproject; import java.util.*; public class MyFirstProject { public static void main(String args[]) { int a=4; if(a==5){ System.out.println("Yes the condition is true"); } else if(a<5) { System.out.println("Your Condtion is possibly true"); }else{ System.out.println("Your Condtion is wrong"); } } } |
Output:
1 |
Your Condition is possibly true |
-
Dangling Else Problem
The Java compiler always link an if with the following else, the problem occurs when the scope is not defined and we put another statement after the first one, as compiler only runs the immediate following statement if the scope is not defined through the parenthesis. this problem can also occur when we define anything outside the scope of if statement and there is an also following the if. this confuses the compiler, in the case of else for the related if statement.
Example:
12345if(value==1)System.out.println("Value is 1");//this statement will be executed and considered within the scope of ifSystem.out.println("If Statement Executed");//this statement is not considered inside the scope of ifelse//confuses the compiler about related ifSystem.out.println("Value is not 1");If Scope is defined:
1234567if(value==1){System.out.println("Value is 1");//this statement will be executed and considered within the scope of if}System.out.println("If Statement Executed");//this statement is not considered inside the scope of ifelse { //confuses the compiler about related ifSystem.out.println("Value is not 1");}
4. Switch Statement:
Switch statement consists of following keywords:
- Switch: where the condition is labeled.
- Case: it will be matched with labeled condition in the switch.
- Break: it will break the code in some case so that other cases will not execute.
- Default: if all the cases are wrong the default will run.
Syntax:
Switch(condition){
Case 1: {
//body here
Break;
}
Case 2: {
//body here
Break;
}
Case 3: {
//body here
Break;
}
Default: {
//body here
Break;
}
}
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 32 33 34 35 36 37 38 39 40 |
package myfirstproject; import java.util.*; public class MyFirstProject { public static void main(String args[]) { int day=4; String dayname=null; switch(day){ case 1: { dayname="Monday"; break; } case 2: { dayname="Tuesday"; break; } case 3: { dayname="Wednesday"; break;} case 4: { dayname="Thursday"; break; } case 5: { dayname="Friday"; break; } case 6: { dayname="Saturday"; break; } case 7: { dayname="Sunday";break; } default: { dayname="You Entered Wrong Day"; } } System.out.println("Day Name is: "+dayname); } } |
Output:
1 |
Day Name is: Thursday |
If you enter day=10 for example, the result would be
1 |
Day Name is: You Entered Wrong Day |
5. Ternary Operator in Java:
The ternary operator is called ternary operator because it has three parts.
Syntax:
Ans here = (condition) ? if true : if false ;
Like take an example:
1 |
Bool x = (2==6 ) ? true : false ; |
Then in above condition it return false and the value of x is false or 0.
Some more examples to define java ternary operator.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package myfirstproject; import java.util.*; public class MyFirstProject { public static void main(String args[]) { int day=10; String dayname=null; dayname = (day<7)? "valid day number " : "invalid day number "; System.out.println(dayname); } } |
Output:
1 |
invalid day number |
Hope you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!