The else keyword in Java programming language
- Home
- Keyword in Java
- The else keyword in Java programming language
- On
- By
- 0 Comment
- 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
Suggested for you