×

Java Else Keyword

  1. The else statement specifies a section of Java code that will run if an if statement's condition is false.
  2. 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.
  3. In if-else statement, the Java keyword or reserved word else is used in executing the opposite statement to the if keyword.
  4. When an if statement is false, the otherwise statement is always carried out. It is a branching statement component that is optional.
  5.  Optional else part statements are carried out when the if statement returns false.

Syntax of an Else Keyword in Java

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 jumps to the else section and performs those statements if the Boolean expression is false.

Simple Example

if(test_expression) {
//truecondition 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 Programs for Else Keyword in Java

Example program 1:

class Else{
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 program 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 program 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 program 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 program 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");
} elseif (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 program 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

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console...

3 minutes read.

Difference between Static and Instance Methods in Java

Static Method in Java The static technique has a place with the class as opposed to the object of the course. These are intended to be divided between every one of...

10 minutes read.

Bouncy Number in Java

We will define bouncy numbers in this section and write Java programmes to determine whether a specific number is bouncy. Java coding exams and academic assignments usually inquire about the...

3 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java Float Keyword

Float: In general, there are two categories of data types: primitive data types and non-primitive data types. So, float is the data type which is a primitive data type. Declarating the variables and...

3 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Multithreading Program in Java

Multithreading Program in Java: Before discussing multithreading, it is important to discuss threads. Threads are the most fundamental part of a process. A process can have one or more threads....

4 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Java Math sinh() Method

The sinh() method of Java Math class returns the hyperbolic sine of the specified double value. Syntax: public static double sinh(double x) Parameters: The parameter ‘a’ represents the number whose hyperbolic sine is to...

2 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java Integer rotateRight() method

The rotateRight() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value right by the specified number of bits. Syntax public...

1 minute read.

How to add Elements in Array in Java

Consider an array of the size n, in the given size we must add the elements in the given array. In this tutorial we are preferred to learn how to add...

3 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

How to check Date is Greater in Java?

In this article, you will be acknowledged about how to check if the given date is greater than the other date or not. We are simply comparing the dates and...

7 minutes read.