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.
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.
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
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…