×

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

Related Topics

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Differences between Lock and Monitor in Java Concurrency

In this tutorial, we will discuss the overview of Lock and Monitor and the differences between them. Introduction Java Concurrency is the ability to perform specific tasks at a time parallelly. The...

4 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.

BigDecimal toString() in Java

BigDecimal is a Java class that is a part of the java.math package and the java.base module. It implements the ComparableBigDecimal> interface and extends the Number class. The BigDecimal class...

3 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.