Java If Keyword
Definition:
- The if statement specifies a section of Java code that will run if an if statement's condition is false.
- The following conditional statements can be used in Java:
- To provide a block of program to run or execute only if a certain condition is true, then we need to use the if block to execute the program.
- If the given or provided condition is false, then we will describe it in the else block of code that should be printed.
- To define various code blocks to be performed, use the switch command.
- In if-else statement, the Java keyword or reserved word else is used in executing the opposite statement to the if keyword.
- When an if statement is false, the otherwise statement is always carried out. It is a branching statement component that is optional.
- Optional else part statements are carried out when the if statement returns false.
Syntax
Below is the declaration or Syntax of an else keyword in java programming language.
if(condition)
{
<state> //the block or portion of if statements
}
else
{
<state> //the portion or part where the else statements are involved or executed.
}
Control flow of the program starts with the if section and performs those statements if the Boolean expression is false.
Simple Example:
if(test_expression) {
//true condition is excuted in this block of statements
System.out.println("True condition: Raju is well characterised person");
}
else {// false conditional statement is executed in this portion
System.out.println("False condition:Raju is not a characterised person");
}
Let us consider the example programs to understand better about else keyword.
Examples for If Keyword in Java
Example 1:
class If{
public static void main (String args []) {
double age=21;
if(age>21) {
//It is executed if condition is true
System.out.println("you are eligible for marriage");
}
else {//executed if the given condition is false statement
System.out.println("you are not eligible for marriage ");
}
}
}
Output of the code is executed; it produced the following result:
You are not eligible for marriage
Example 2:
//A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number
public class If_lseEx {
public static void main (String [] s) {
//variable declaration
int integer=13;
///Verify whether the number is divisible by two.
if(integer%2==0) {
System.out.println("Given integer is even");
} else {
System.out.println("Given integer is odd");
}
}
}
Output of the given program is:
Given integer is odd
Example 3:
Example program of leap year
If a year can be divided by 4 and 400, it is a leap year. not by 100, though.
public class LeapYearEx3 {
public static void main (String [] s) {
double year=2020.
if (((year % 4 ==0) && (year % 100! =0)) || (year % 400==0)) {
System.out.println("The year is a leap year");
}
else {
System.out.println("The year is not a leap year");
}
}
}
Output of the program is given by:
The year is a leap year
Example 4:
/Java programme to show how the If-else-if ladder is used.
/
It is a programme that assigns grades for failure, a D, a C, a B, an A, and an A+.
public class If_Else_IfEx4 {
public static void main (String [] s) {
int score=83;
if(score<50) {
System.out.println("unsuccess");
}
else if(score>=50 &&score<60) {
System.out.println("D points");
}
else if(score>=60 &&score<70) {
System.out.println("C points");
}
else if(score>=70 &&score<80) {
System.out.println("B points");
}
else if(score>=80 &&score<90) {
System.out.println("A points");
} else if(score>=90 &&score<100) {
System.out.println("A+ points");
} else {
System.out.println("Invalid statement!");
}
}
}
Output of the program is:
C points
Example 5:
Program to check whether a given number is POSITIVE, NEGATIVE or ZERO:
public class P_N_Ex5 {
public static void main (String [] s) {
int Integer=-182;
if (Integer>0) {
System.out.println("The number is positive");
} else if (Integer<0) {
System.out.println("The number is negative");
} else {
System.out.println("The number is zero");
}
}
}
Output of the program is
The number is negative.
Example 6:
/Java Program to Show Nested If Statements in Action.
public class NestedIfEx6 {
public static void main (String [] s) {
doubleyears=20;
doubleheight=120;
//applying condition on years and height
if(years>=18) {
if(height>50) {
System.out.println("can donate the blood");
}
}
}}
Output of the program is:
Can donate the blood