ELSE IF LADDER CONDITION WITH JAVA PROGRAM

Elseif Ladder Condition with Java Program

“ElseIf Ladder condition” is very popular in condition with java program. If we want to check more than 10 conditions, then we have to use “Elseif Ladder condition” in our java program.

 

ElseIf Ladder Condition

else if ladder condition with java program

 

In this image, we can easily understand the “elseif ladder” condition. If condition1 is true then program’s flow goes to Result Statement otherwise goes to Con-2. If Cond-2 is true then Result Statement executes otherwise goes to Con-3. So and so in the else statement we can get final result if all condition is false.

 

How to use return in method

In our last blogs we did not introduce return in any method. In this blog we introduce return int type method. In this program we have findAnswer (int, int, char) method with int return type. In this method ans is one variable, which will return after execution in all function. Let’s see the program on “else if ladder condition with java program”.   

 

Program in “Else if ladder condition”.

import java.util.*;

class condition3

{

static void function1(int x,int y, char op) // Static method

{

int ans=0;

if(op==’+’)

{

ans = x+y;

}

else if(op==’-’)

{

ans = x-y;

}

else if(op == ‘>’)

{

ans = (x>y) ? x : y;

}

else

{

ans = 0;

}

return ans;

}

public static void main(String args[])

{

int x,y,answer;

char ch;

Scanner in1 = new Scanner(System.in);

System.out.print(“Enter X : ”);

x = in1.nextInt(); // Method

System.out.print(“Enter Y : ”);

y = in1.nextInt();  // Method

System.out.print(“Enter Operator : ”);

ch = in1.next().charAt(0);       // find first char of string

answer = findAnswer(x,y,ch);

System.out.println(“Answer = ” + answer);

}

}

Output:

Enter X: 34

Enter Y: 20

Enter Operator: +

Answer: 54

 

Video available on YouTube.

 

 

You can also download: Click Here -> Download Program


[sbscrbr_form]

If you have problem on this blog, you can directly comment or subscribe website.


 

889total visits,2visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

2 Comments

  1. August 30, 2016 - 3:14 pm

    Kya baat..! Kya baat..! Kya baat..!

    • mayank
      August 30, 2016 - 3:39 pm

      Joyu 6 DID, joyu 6…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image