Slide 1
Slide 2
Slide 3

Miscellaneous Operators in java programming (V22)

No Comments

Miscellaneous Operators (Ternary Operators)

The conditional operator is also known as the ternary operator. This operator has three operands and is used to evaluate a Boolean expression. The purpose of this operator is to determine which value should be assigned to a variable.

Syntax: Conditional Operator ( ? : )

Source Code:

Copied!
public class TernaryOperator {
    public static void main(String[] args) {
        int a = 25, b = 20;

        // Using Ternary Operator
        int min = (a < b) ? a : b;

        System.out.println("Minimum value: " + min);
    }
}

Result:

Source Code:

Copied!
import java.util.Scanner;
public class EvenOddTernary {
    public static void main(String[] args) {
    	Scanner scanner = new Scanner(System.in);
        int num;
		System.out.print("Enter number: ");
		num = scanner.nextInt();
        // Using Ternary Operator
        String result = (num % 2 == 0) ? "Even" : "Odd";

        System.out.println("\nThe number is: " + result);
    }
}

Result:

Source Code:

Copied!
public class TernaryMax {
    public static void main(String[] args) {
        int A = 78, B = 79, C = 80;

        // Using Nested Ternary Operator
        int max = (A > B) ? ((A > C) ? A : C) : ((B > C) ? B : C);

        System.out.println("Maximum value: " + max);
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top