--- id: cpp-conditions-shorthand title: "C++ Short Hand If Else" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["ternary operator", "nested ternary", "C++ 삼항 연산자"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.85 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "conditions", "ternary-operator"] raw_sources: ["https://www.w3schools.com/cpp/cpp_conditions_shorthand.asp"] applied_in: [] github_commit: "" --- # [[CPP Short Hand If Else]] ## 🎯 한 줄 통찰 (One-line insight) Unlike C's equivalent chapter (which only replaces two `printf()` calls with a ternary), C++'s version goes one step further with NESTED ternaries — chaining `(time < 12) ? "Good morning." : (time < 18) ? "Good afternoon." : "Good evening.";` to handle THREE outcomes in one expression — while explicitly warning this makes code harder to read, showing C++'s string type enables a genuinely richer ternary use case (storing a computed STRING result) that C's more limited ternary (usually just printing directly) didn't showcase. [S1] ## 🧠 핵심 개념 (Core concepts) - **Ternary operator (`?:`)** — a shorthand if/else that RETURNS a value; syntax: `variable = (condition) ? expressionTrue : expressionFalse;`. [S1] - **Storing the result in a `string` variable** — `string result = (time < 18) ? "Good day." : "Good evening.";` — leverages C++'s string type directly. [S1] - **Using it inline inside `cout`** — `cout << ((time < 18) ? "Good day." : "Good evening.");` — no intermediate variable needed. [S1] - **Nested ternary** — chaining multiple `?:` expressions to handle MORE than two outcomes, at the cost of readability. [S1] ## 📖 세부 내용 (Details) - Storing the ternary result in a string: `int time = 20; string result = (time < 18) ? "Good day." : "Good evening."; cout << result;`. [S1] - Inline ternary directly inside cout: `cout << ((time < 18) ? "Good day." : "Good evening.");`. [S1] - Nested ternary handling three outcomes: `int time = 22; string message = (time < 12) ? "Good morning." : (time < 18) ? "Good afternoon." : "Good evening."; cout << message;`. [S1] ## ⚖️ 모순 및 업데이트 (Contradictions & updates) - **중첩 삼항 연산자는 가독성 저하 경고와 함께 사용 가능**: 세 가지 이상의 결과를 하나의 삼항 표현식으로 처리할 수 있지만, 명확성을 위해 보통은 일반 if...else if...else 문을 쓰는 것이 낫다고 명시적으로 권고됨. [S1] ## 🛠️ 적용 사례 (Applied in summary) 현재 발견된 실제 적용 사례가 없습니다 — 짝수/홀수 판정 결과를 삼항 연산자로 문자열에 담는 연습문제가 실전 활용의 대표 사례로 제시됨. [S1] ## 💻 코드 패턴 (Code patterns) Nested ternary operators handling three possible outcomes (C++): ```cpp int time = 22; string message = (time < 12) ? "Good morning." : (time < 18) ? "Good afternoon." : "Good evening."; cout << message; ``` ## ✅ 검증 상태 및 신뢰도 - **상태:** draft - **검증 단계:** conceptual - **출처 신뢰도:** B (W3Schools — widely used educational reference, not a primary standards body) - **신뢰 점수:** 0.85 - **중복 검사 결과:** 신규 생성 (New discovery) ## 🔗 지식 그래프 (Knowledge Graph) - **상위/루트:** [[C++ Tutorial]] - **관련 개념:** [[CPP Conditions ElseIf]], [[CPP Conditions Logical]], [[C Conditions Short Hand]] - **참조 맥락:** 삼항 연산자 — 조건문 내 논리 연산자(Conditions Logical) 챕터로 이어짐. ## 📚 출처 (Sources) - [S1] W3Schools — C++ Short Hand If Else — https://www.w3schools.com/cpp/cpp_conditions_shorthand.asp ## 📝 변경 이력 (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Short Hand If Else" page (Astra wiki-curation, P-Reinforce v3.1 format).