×

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

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Tree Implementation in Java

Introduction to Tree A non-linear, hierarchical data structure called a "tree" is made up of a number of nodes, each of which contains a values and a sequence of pointers to...

14 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Advantages and Disadvantages of Strings in Java

What is a String? Java is an object-oriented, platform-independent, high-level, general-purpose, and interpreted programming language.Sun Microsystems is known as the founder of java in 1991; the Java programming language was developed...

4 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.