Home Help Java E The else Keyword
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

The else Keyword

Advertisement
The else is the one more keyword of Java. It is used to specify a statement or block of statements that are executed when the condition for testing specified by the if keyword evaluate to false.

The else Keyword 

     

The else is the one more keyword of Java. It is used to specify a statement or block of statements that are executed when the condition for testing specified by the if keyword evaluate to false. When the if condition evaluates to false, the code after the else keyword is executed. It is an optional part of a branching statement that starts the 'false' statement block. else  is a java keyword that may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program.

The following shows the format of the else selection Construct syntax. 

if (<condition>)

{

 <statements1>

<statements2>

}

 else

{

 <else statements1>

<else statements2>

}

 

When a if selection is encountered during the program's execution, the condition specified by the <condition> is evaluated. Once the condition is met, the statement specified in the if block are executed; then after, the program flow jumps to the end of the if selection. If the condition was not met, then the program flow jumps to the else block, at which point, the statements in the <elsestatements> element are executed.

For instance,

class ElseDemo{
  public static void main(String[] args){ 

  if (x<1){
   System.out.println("smaller than 1");
 }
   else{
   System.out.println("greater than 1");
  }

   }

}

 The else keyword can also be used with multiple if conditions. For example: 

if (<condition1>){

 <statements>

}

else if (<condition2>){

 <statementsX>

}

else{

 <else statements>

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 3, 2006

Ask Questions?    Discuss: The else Keyword  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com