Categories: Keyword in Java

The else keyword in Java programming language

The else keyword in Java programming language

We will discuss the else keyword in Java programming language

else is a Java keyword used in association with the if keyword in an if-else statement. Else statements always execute when if statements isn’t true. It is an optional part of a branching statement. When the if statement is false, optional else part statements are executed.

Declaration

Syntax
if(condition)
{
<statements>  //statements belongs to if part
}
else
{
<statements>  //statements belongs to else part
}

When the boolean expression is true, the statement specified in the if block is invoked. If the boolean expression is false, control flow jumps to the else part and executes those statements.

Example
if(test_expression){
//when if condition is true this statement is excuted
System.out.println("if condition is true this part is executes");
}
else{ //when if condition is false this statement is excuted
System.out.println("if condition is false this part is executes");
}

 Program 1

class Elseinjava{
public static void main(String args[]){
    int age=18;

if(age>18){
//when if condition is true this statement is excuted
System.out.println("you can vote election");
}
else{ //when if condition is false this statement is excuted
System.out.println("You can not vote the election");
}

}
}

When the above code is compiled and executed, it will produce the following results

You can not vote the election

 

There are other Java language keywords that are similar to this keyword

try  in Java

Catch  in Java

implements  in java

public  in Java

static  in Java

Volatile  in Java

Transient  in Java

Package  in Java

class  in Java

boolean  in Java

break  in Java

do  in Java

double  in Java

This  in Java

final  in Java

switch  in Java

interface  in Java

extends  in Java language

 

Suggested for you

 Java language Keywords

C language Keywords

 Python language Keywords

keywords in C language

Usage of final in Java

Usage of this  in Java

Finally  in Java language

 

The if keyword in Java programming language
The switch keyword in Java programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago