조건부 연산자

(Ternary operator에서 넘어옴)
conditional operator, inline if; iif, ternary if, ternary operator
조건부 연산자, 비교 선택 연산자, 3항 연산자, 삼항 연산자
? :

1 C[ | ]

C
CPU
0.0s
MEM
17M
0.1s
Copy
#include <stdio.h>
int main()
{
    int a = 2, b = 1;
    int max = a > b ? a : b;
    printf("%d\n", max);
}
2

2 Java[ | ]

Java
Copy
int max;
int a = 1;
int b = 2;
max = (a > b) ? a : b;
System.out.println(max);

3 JavaScript[ | ]

JavaScript
Copy
console.log( "The fee is " + (isMember ? "$2.00" : "$10.00") );

4 PHP[ | ]

PHP
Copy
$score = 50;
$grade = $score > 80 ? 'A' : 'B';
echo $grade;
# B

5 Python[ | ]

Python
Copy
score = 50
grade = 'A' if score > 80 else 'B'
print( grade )
# B

6 Ruby[ | ]

Ruby
Copy
puts 1 == 2 ? "true" : "false"
# false

7 같이 보기[ | ]

8 참고[ | ]