Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- 문자열
- pointer
- 백준
- 함수
- string
- predictive analysis
- 포인터
- Pre-processing
- pass by reference
- baekjoon
- 반복문
- Object Oriented Programming
- vscode
- 배열
- 알고리즘
- assignment operator
- array
- C++
- Data Science
- 오블완
- const
- function
- programming
- 티스토리챌린지
- Deep Learning
- Python
- raw data
- Class
- OOP
- 파이썬
Archives
- Today
- Total
Channi Studies
[C++] Conditional Operator (조건 연산자) 본문
Conditional Operator는 다음과 같은 형식으로 사용됩니다.
variable = (cond_expr) ? expr1 : expr2
다음은 예시 코드입니다.
#include <iostream>
using namespace std;
int main(){
int a{10}, b{20};
int score {92};
int result {};
// a > b 이면 result = a, b > a 이면 result = b
result = (a > b) ? a : b;
// score > 90 이면 Excellent! 출력
// score <= 90 이면 Good~ 출력
cout << ((score > 90) ? "Excellent!" : "Good~") << endl;
return 0;
}
위 코드에서 result = (a > b) ? a : b 는 다음과 같습니다.
// result = (a > b) ? a : b; 는 다음과 동일합니다
if (a > b) {
result = a;
}else{
result = b;
}
여러 줄의 if-else문을 한줄로 치환할 수 있기 때문에,
적재적소에 활용한다면 코드가 더욱 간략해 질 수 있습니다.
'C++ > 반복문 (Loop)' 카테고리의 다른 글
| [C++] do-while Loop (0) | 2023.12.12 |
|---|---|
| [C++] While Loop (0) | 2023.12.10 |
| [C++] Range-based for Loop (0) | 2023.12.10 |