1// (condition) ? (if true, do this) : (otherwise, do this)
2const hasAccount = true
3// let userDisplayMessage
4// if (hasAccount) {
5// userDisplayMessage = 'Welcome Back'
6// } else {
7// userDisplayMessage = 'Please Sign Up'
8// }
9const userDisplayMessage = hasAccount ? 'Welcome Back' : 'Please Sign Up'
10console.log(userDisplayMessage)
1// It is bascially an if else statement but is an operator
2// Heres a program to get the largest of 3 numbers
3import java.util.Scanner;
4
5 public class Program_19{
6 static void main(){
7
8 Scanner sc = new Scanner(System.in);
9
10 System.out.println("Enter Number 1 [int]: ");
11
12 int num1 = sc.nextInt();
13
14 System.out.println("Enter Number 2 [int]: ");
15
16 int num2 = sc.nextInt();
17
18 System.out.println("Enter Number 3 [int]: ");
19
20 int num3 = sc.nextInt();
21
22 int largest = (num1>num2)? ((num1>num3)?num1:num3) : ((num2>num3)? num2:num3);
23
24 System.out.println(largest+" is the largest among the 3 numbers");
25
26 }
27
28}
1condition ? expression-if-true : expression-if-false;
2
3function findGreater(a, b) {
4 return a > b ? "a is greater" : "b is greater";
5}
1// syntax:
2condition ? console.log("true") : console.log("false");
3// e.g:
4let n = 15;
5n % 2 === 0 ? console.log("even number") : console.log("odd number");
1double sinc(double x) => x != 0.0 ? Math.Sin(x) / x : 1;
2
3Console.WriteLine(sinc(0.1));
4Console.WriteLine(sinc(0.0));
5// Output:
6// 0.998334166468282
7// 1
1isMember ? '$2.00' : '$10.00'
2// isMember true => output: "$2.00"
3
4// isMember false => output: "$10.00"
5
6// isMember null => output: "$10.00"