--- id: cpp-switch title: "C++ Switch" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["switch case", "break keyword", "default keyword", "C++ switch ๋ฌธ"] 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", "switch"] raw_sources: ["https://www.w3schools.com/cpp/cpp_switch.asp"] applied_in: [] github_commit: "" --- # [[CPP Switch]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) The syntax overview explicitly labels `break` and `default` as "optional" โ€” a framing C's chapter never used โ€” yet the same fall-through risk applies identically: without `break`, execution continues into subsequent cases regardless of match, meaning "optional" describes their SYNTACTIC status (the switch compiles without them) rather than their PRACTICAL necessity for correct behavior. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **`switch (expression)`** โ€” evaluates the expression ONCE, then compares it against each `case` value. [S1] - **`case x:`** โ€” a candidate value to match; its block runs if the switch expression equals `x`. [S1] - **`break`** โ€” exits the switch block once a match is handled; described as OPTIONAL syntactically, though omitting it causes execution to fall through into later cases. [S1] - **`default:`** โ€” runs if no case matches; also optional. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Weekday-name lookup via switch: `int day = 4; switch (day) { case 1: cout << "Monday"; break; ... case 4: cout << "Thursday"; break; ... }` โ€” outputs "Thursday" for day 4. [S1] - Using default for unmatched cases: `int day = 4; switch (day) { case 6: cout << "Today is Saturday"; break; case 7: cout << "Today is Sunday"; break; default: cout << "Looking forward to the Weekend"; }` โ€” day 4 matches neither case 6 nor 7, so default runs. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **break์™€ default๋Š” "์„ ํƒ ์‚ฌํ•ญ"์ด์ง€๋งŒ ์‹ค์งˆ์ ์œผ๋กœ๋Š” ๊ฑฐ์˜ ํ•„์ˆ˜**: ๋ฌธ๋ฒ•์ ์œผ๋กœ๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ช…์‹œ๋˜์ง€๋งŒ, break๊ฐ€ ์—†์œผ๋ฉด ์ดํ›„ case๋กœ ๊ณ„์† ์‹คํ–‰์ด ์ด์–ด์ง€๋Š”(fall-through) ๋ฌธ์ œ๊ฐ€ C ์ฑ•ํ„ฐ์™€ ๋™์ผํ•˜๊ฒŒ ์กด์žฌํ•จ์ด ํ™•์ธ๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ์š”์ผ ๋ฒˆํ˜ธ(1-7)๋ฅผ ์š”์ผ ์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์ œ์™€, ์ฃผ๋ง์ด ์•„๋‹Œ ์š”์ผ์— ๋Œ€ํ•ด default๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ ์›๋ฌธ์—์„œ ์ง์ ‘ ์‹ค์ „ ํ™œ์šฉ ์‚ฌ๋ก€๋กœ ์ œ์‹œ๋จ. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Using default to handle any value not matched by an explicit case (C++): ```cpp int day = 4; switch (day) { case 6: cout << "Today is Saturday"; break; case 7: cout << "Today is Sunday"; break; default: cout << "Looking forward to the Weekend"; } // Outputs "Looking forward to the Weekend" ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.85 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[C++ Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[CPP Conditions RealLife]], [[CPP While Loop]], [[C Switch]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** switch ์„น์…˜ ์œ ์ผ ์ฑ•ํ„ฐ โ€” ๋ฐ˜๋ณต๋ฌธ(Loops) ์„น์…˜์˜ while ๋ฃจํ”„(While Loop) ์ฑ•ํ„ฐ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C++ Switch โ€” https://www.w3schools.com/cpp/cpp_switch.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ Switch" page (Astra wiki-curation, P-Reinforce v3.1 format).