There are various types of if statement in Java…
· if statement
· if-else statement
· if-else-if ladder
· nested if statement
if Statement
Use if to specify a block of code to be executed, if a
specified condition is true.
Syntax:
}
public class IfExample {
public static void main(String[ ] args) {
//defining a ‘gpa’ variable
if(gpa>7.5) {
System.out.print(“gpa is greater than 7.5”);
}
}
Note: if is in lowercase letters. Uppercase letters (IF or If)
will generate an error.
Output
gpa is greater than 7.5
if-else Statement
- Use if to specify a
block of code to be executed, if a specified condition is true.
- Use else to specify a
block of code to be executed, if the same condition is false.
Syntax:
}
else {
}
Example
//It is a Program of odd and even number
public class IfElseExample {
public static void main(String[ ] args) {
//defining a variable
int n=6;
//n is even if it is divisible by 2
if(n%2==0)
System.out.println(n + “ is an even number”);
System.out.println(n + “ is a odd number”);
}
Output
6 is an even number
Leap Year Example
A year is a leap, if it is divisible by 4 and 400. But not by 100.
public static void main(String[ ] args) {
//defining a variable
int year=2020;
System.out.println(year + “ is a leap year”);
System.out.println(year + “ is not a leap year”);
}
Output
2020 is a leap year
Using Ternary Operator
We can also use ternary operator (? :) to perform the task of
if-else statement.
It is a short hand way to check the condition.
If the condition is true, the result of ? is returned. But, if the
condition is false, the result of : is returned.
Example
public static void main(String[ ] args) {
double number= -9.52;
}
Output
-9.52 is not positive
if-else-if ladder Statement
The if-else-if ladder
statement executes one condition from multiple statements.
Syntax:
}
else if (condition2) {
}
Else if (condition3) {
}
…..
else {
}
Example
//It is a Program of grading system for A+, A, B, c, D, E grade and Fail
public class IfElseExample {
public static void main(String[ ] args) {
int marks=95;
System.out.println(“A+ grade”);
System.out.println(“A grade”);
System.out.println(“B grade”);
System.out.println(“C grade”);
System.out.println(“D grade”);
System.out.println(“E grade”);
System.out.println(“Fail”);
}
Output
A+ grade
Program to check Positive, Negative or Zero
public static void main(String[ ] args) {
int number=-6;
System.out.println(“Positive”);
System.out.println(“Negative”);
System.out.println(“Zero”);
}
Output
Negative
Nested if statement
The nested if statement
represents the if block within another if block.
Here, the inner if block condition executes only when outer if
block condition is true.
Syntax:
if (condition) {
}
public class NestedIfExample {
public static void main(String[ ] args) {
//Creating two variable for age and weight
int age=19;
if(weight>=50)
System.out.println(“You are eligible to donate blood”);
System.out.println(“You are not eligible to donate blood”);
else
System.out.println(“Age must be greater than 18”);
}
Output
You are eligible to
donate blood
👌👌..good going
ReplyDeleteKeep going..������������
ReplyDeleteKeep going ❣️
ReplyDelete