Introduction:
If-else statement in java
Java if statement can generally used for checking the condition. It is supposed to check boolean condition that can be true or false. There is the presence of various types of if statements in the Java programming language.
They are:
The if statement
The if-else statement
The if-else-if ladder
The nested if statement \
Java if statement:
This condition is supposed to be testing some conditions. It is going to execute the if block when the situation arrives where the condition gets true.
Syntax:
if(condition){
//code that we want to be executed
}
If-else statement in java
Example:
public class ri_IfExample {
public static void main(String[] args) {
//define the an 'ri_age' variable
int ri_age=20;
//checking statement(condition)
if(age>18){
System.out.print("The person is above 18");
}
}
}
Output:
The person is above 18
If-Else Statement In Java:
This statement present in Java programming language is also supposed to be checking the condition. If it will execute the if block the if condition will be considered to be true but on the other hand the else statement will be considered to be true.
Syntax:
if(condition){
//code that gets executed when condition is true
}else{
//code that gets executed when condition is false
}
public class ri_IfElseExample {
public static void main(String[] args) {
int ri_number=15;
//Checking the condition if the number here is divisible by 2 or not
if(ri_number%2==0){
System.out.println("This is even number");
}else{
System.out.println("This is odd number");
}
}
}
if-else statement in java
Output:
This is odd number
Using Ternary Operator:
The ternary operator can be considered to be a shorthand method to write down the if else statement. It can be denotes as (? 🙂 and is supposed to be executing the same tasks as the if else statements.
Example:
public class ri_IfElseTernaryExample {
public static void main(String[] args) {
int ri_number=13;
//Using our ternary operator
String ri_output=(number%2==0)?"This is even number":"This is odd number";
System.out.println(ri_output);
}
}
Output:
This is odd number
If-Else Statement In Java ladder statement:
This is supposed to be executing one condition from multiple statements in the Java programming language.
Syntax:
if(1st condition){
//code that we want to be executed if 1st condition is true
}else if(2nd condition){
//code that we want to be executed if 2nd condition is true
}
else if(3rd condition){
//code that we want to be executed if 3rd condition is true
}
...
else{
//code that we want to be executed if all the conditions are false
}
public class ri_PositiveNegativeExample {
public static void main(String[] args) {
int ri_number=-15;
if(ri_number>0){
System.out.println("This number is Positive");
}else if(ri_number<0){
System.out.println(“This number isNegative");
}else{
System.out.println("This number is Zero");
}
}
}
if-else statement in java
Output:
Negative
Java Nested if statement:
The nested if statement in the Java programming language is supposed to be representing the if blocks that is inside of the if blocks. Here the inner block gets executed when the outer block’s condition gets true.
Syntax:
if(ri_condition){
//code that we want to be executed
if(ri_condition){
//code that we want to be executed
}
}
if-else statement in java