Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

else if ladder


Else-If Ladder

Else if ladder is a multi-path decision making statement.It contains many number of conditions but when the compiler founds a certain condition which is to be true then it executes that statement only.

We can say it as a multi-path decision chain of if’s.

Syntax

if( condition1 ) 
{
// condition1 if block
}
else if( condition2 )
{
// condition2 if block
}
else if( condition n)
{
//condition n if block
}
else
{
// else block
}

How else-if ladder works? -

  • Compiler first starts to evaluate the conditions from the first statement of the if and then goes executing one by one downwards.
  • As soon as the true condition is found, the statement associated with it is executed and the control is transferred to the statement of that block.
  • When all conditions of the else-if ladder becomes false, then the final else means default block statement is executed.

Example - Calculating grades of student based on Obtained Marks.

In this example we are displaying the grades of student by accepting throw commandline argument
Assume we are passing 90

class elseIfLadder
{
   public static void  main(String args[])
   {
      int marks = Integer.parseInt(args[0]);
      char grade;
		
      if (marks < 35)
         grade = 'F';
      else if (marks >= 35 && marks <= 49)
	 grade = 'C';
      else if (marks >= 50 && marks <= 60)
	 grade = 'B';
      else if (marks >= 61 && marks <= 100)
	 grade = 'A';
      else
	 grade = 'F';
			
      System.out.println("You got : "+grade);
   }
}
Note : If there is only one statement in the block then it can be written without curly brackets.

Output

You got : 90
Best Examples for Practice
  • Calculator program using else-if ladder
  • Check Number is Even or Odd program
  • Check number is Positive or Negative program
  • Find largest number from two/three numbers

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo