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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago