If... else condition statements are the most common form of condition statements, which selectively process certain conditions. It usually shows that "if a certain condition is met, some processing will be performed; otherwise, another processing will be performed ".
Syntax
If (booleanExpression ){
Statement (s)
}
Or
If (booleanExpression ){
Statement (s)
} Else {
Statement (s)
}
For example, in the following statement, if the block is executed, if x is greater than 4.
Public class MainClass {
Public static void main (String [] args ){
Int x = 9;
If (x> 4 ){
// Statements
}
}
}
In the following example, if the block is executed, a ratio of 3 is returned. Otherwise, other blocks will be executed.
Public class MainClass {
Public static void main (String [] args ){
Int a = 3;
If (a> 3 ){
// Statements
} Else {
// Statements
}
}
}
If the expression is too long, you can use two units of indentation for subsequent lines.
Public class MainClass {
Public static void main (String [] args ){
Int numberOfLoginAttempts = 10;
Int numberOfMinimumLoginAttempts = 12;
Int numberOfMaximumLoginAttempts = 13;
Int y = 3;
If (numberOfLoginAttempts <numberOfMaximumLoginAttempts
| NumberOfMinimumLoginAttempts> y ){
Y ++;
}
}
}
If there is only one statement
Public class MainClass {
Public static void main (String [] args ){
Int a = 3;
If (a> 3)
A ++;
Else
A = 3;
}
}
Consider this example:
Public class MainClass {
Public static void main (String [] args ){
Int a = 3, B = 1;
If (a> 0 | B <5)
If (a> 2)
System. out. println ("a> 2 ");
Else
System. out. println ("a <2 ");
}
}
Multiple if else
Public class MainClass {
Public static void main (String [] args ){
Int a = 3, B = 1;
If (a> 0 | B <5 ){
If (a> 2 ){
System. out. println ("a> 2 ");
} Else {
System. out. println ("a <2 ");
}
}
}
}