
Other Operators 3.17
else
marks +=2;
Java language gives you the facility of conditional operator, using which the above 4 lines can
be coded as a single line
marks = (attendance > 100) ? marks + 10 : marks + 2;
The syntax is : z = exp1 ? expr 2 : exp3. Exp1 is evaluated first. If it is true z is
equated to the result of exp2 . else z is equated to exp3.
Example 3.7: Ternary.java To Find Out Maximum of Two Numbers and Three Numbers
1.
package com.oops.chap3;
2. public class Ternary {
3. public static void main(String[] args) {
4. byte a=56, b=101, c=45, max;
5. // find the maximum of a & b
6. max=(a>b)?a:b;
7. System.out.println(“maximum ...