Wednesday 22 May 2013

CONDITIONAL OPERATOR

This post is specifically concentrated on the inline conditions that are implemented by ? : operator. ? : is conditional operator that can be efficiently used as an substitute for small if else blocks.
The following programs lets the user to input two numbers and then the output is the greater of the two numbers.

Source Code:-
/*******************************************************************************/

import java.util.Scanner;
public class inlinecondition {


public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
System.out.println("ENTER VALUE OF a");
int a=s.nextInt();

System.out.println("ENTER VALUE OF b");
int b=s.nextInt();

int max1=(a>b)?a:b;
System.out.println("ON USING INLINE CONDITION THE RESULT IS :");
System.out.println("Max = "+max1);


//using if else condition to perform the same task...
int max2;
System.out.println("ON USING THE IF ELSE BLOCK THE RESULT IS :");
if(a>b)
max2=a;
else
max2=b;

System.out.println("Max = "+max2);


}

}
/****************************************************************************/

No comments:

Post a Comment