0% found this document useful (0 votes)
30 views9 pages

17.ternary Operator

The document discusses the ternary operator in Java, which is used for conditional execution of expressions. It provides multiple examples of using the ternary operator to find the greatest of two or four numbers, calculate net salary based on basic salary, and compute electricity bill amounts based on units consumed. Each example includes user input and expected output to illustrate the functionality of the ternary operator.

Uploaded by

Sujay Sabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

17.ternary Operator

The document discusses the ternary operator in Java, which is used for conditional execution of expressions. It provides multiple examples of using the ternary operator to find the greatest of two or four numbers, calculate net salary based on basic salary, and compute electricity bill amounts based on units consumed. Each example includes user input and expected output to illustrate the functionality of the ternary operator.

Uploaded by

Sujay Sabale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Todays Topic

• Ternary Operator
Ternary Operator
The ternary operator (?) is used to conditionally
execute simple expressions.

condition ? expression1 : expression2

First condition is checked if it is true then expression1 is


evaluated else expression2 is evaluated.
import java.util.Scanner;
class demo
{ WAP to read 2 nos and
public static void main(String args[]) find greatest of them.
{
Scanner stdin=new Scanner(System.in);
System.out.println(“Enter 2 nos :”);
int a=stdin.nextInt( );
int b=stdin.nextInt( ); Output :
Enter 2 nos:
if ( a>b ) 5 12
System.out.println(“Greatest Number is “ + a ); Greatest Number is 12
else
System.out.println(“Greatest Number is “ + b );
}
}
import java.util.Scanner;
class demo
{ WAP to read 2 nos and
public static void main(String args[]) find greatest of them.
{
Scanner stdin=new Scanner(System.in); Output :
System.out.println(“Enter 2 nos :”); Enter 2 nos:
int a=stdin.nextInt( ); 5 12
int b=stdin.nextInt( ); Greatest Number is 12
condition ? expression1 : expression2
int m = a > b ? a : b ;

System.out.println(“Greatest Number is “+m);


}
}
import java.util.Scanner;
class demo
WAP to read 2 nos and
{
find greatest of them.
public static void main(String args[])
{ Output :
Scanner stdin=new Scanner(System.in); Enter 2 nos:
System.out.println(“Enter 2 nos :”); 5 12
int a=stdin.nextInt( ); Greatest Number is 12
int b=stdin.nextInt( );
condition ? expression1 : expression2
System.out.println( “Greatest Number is “ + ( a>b ? a : b ) ) ;
}
}
import java.util.Scanner;
class demo
{ WAP to read 4 nos and
public static void main(String args[]) find greatest of them.
{
Scanner stdin=new Scanner(System.in); Output :
System.out.println(“Enter 4 nos :”); Enter 4 nos:
int a=stdin.nextInt( ); a=5 b=12 5 12 23 7
int b=stdin.nextInt( ); Greatest Number is 23
c=23 d=7
int c=stdin.nextInt( );
int d=stdin.nextInt( );
X=12
int x = a > b ? a : b ;
int y = c > d ? c : d ; Y=23
int z = x > y ? x : y ; Z=23
System.out.println(“Greatest Number is “+z);
}
}
import java.util.Scanner;
WAP to read Basic Salary of person and calculate
class demo netSalary according to following criteria.
{
public static void main(String args[]) NetSal=Basic +HRA +TA- Itax HRA BASIC
5000 >=10000
{
3000 <10000
Scanner stdin=new Scanner(System.in);
Basic = 17000 TA BASIC
System.out.println(“Enter Basic :”);
2000 >=15000
int basic=stdin.nextInt( ); hra = 5000
1000 <15000
int hra = basic > 10000 ? 5000 : 3000 ; ta = 2000
int ta = basic > 15000 ? 2000 : 1000 ; ITax BASIC
int itax = basic > 20000 ? 2500 : 0 ; itax = 0 2500 >=20000
int netsal = basic + hra + ta – itax ; netsal = 24000 0 <20000

Output :
System.out.println(“Net Salary is “ + netsal ); Enter Basic Salary: 17000
} Net Salary is 24000
}
import java.util.Scanner; WAP to read Electricity Units and calculate
class demo BillAmt according to following criteria.
{ BillAmt=Units*Rate +ExtraChanges
public static void main(String args[])
Rate Units ExtraCharges Units
{
5 >=300 500 >=1000
Scanner stdin=new Scanner(System.in);
3 <300 0 <1000
System.out.println(“Enter Units :”);
int units=stdin.nextInt( ); units = 600
int rate = units > 300 ? 5 : 3 ; rate = 5
int extracharges = units > 1000 ? 500 : 0 ; extracharges=0
int billamt = units * rate + extracharges ; billamt = 3000

System.out.println(“Bill Amount is “ + billamt ); Output :


} Enter Units: 600
} Bill Amount is 3000
Todays Topic End

You might also like