Example: Java Ternary Operator import java.util.Scanner; class Main { public static void main(String[] args) { // take input from users Scanner input = new Scanner(System.in); System.out.println("Enter your marks: "); double marks = input.nextDouble(); // ternary operator checks if /...
The ternary operator takes3 operands(condition,expression1andexpression2). Hence, the nameternary operator. Example: C++ Ternary Operator #include<iostream>#include<string>usingnamespacestd;intmain(){doublemarks;// take input from userscout<<"Enter your marks: ";cin>> marks;// ternary operator c...
Here, both programs give the same output. However, the use of the ternary operator makes our code more readable and clean. Nested Ternary Operators We can use one ternary operator inside another ternary operator. This is called a nested ternary operator in Swift. For example, // program to ...