조건부 연산자

conditional operator, inline if; iif, ternary if, ternary operator
조건부 연산자, 비교 선택 연산자, 3항 연산자, 삼항 연산자
? :

1 C[ | ]

#include <stdio.h>
int main()
{
    int a = 2, b = 1;
    int max = a > b ? a : b;
    printf("%d\n", max);
}

2 Java[ | ]

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

3 JavaScript[ | ]

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

4 PHP[ | ]

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

5 Python[ | ]

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

6 Ruby[ | ]

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

7 같이 보기[ | ]

8 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}