Site icon Codeforcoding

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
Exit mobile version