--- id: c-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.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "switch"] raw_sources: ["https://www.w3schools.com/c/c_switch.php"] applied_in: [] github_commit: "" --- # [[C Switch]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) `break` isn't optional cleanup โ€” without it, C would keep executing every subsequent `case` block regardless of whether it matches, since `switch` on its own only decides WHERE to jump in, not where to stop; the source frames `break` as actively saving execution time by "ignoring" the rest of the switch block once a match is handled, implying that omitting it (fall-through) is a real and easy-to-make mistake in C, unlike languages where each case implicitly breaks. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **`switch (expression)`** โ€” evaluates the expression ONCE, then compares it against each `case` value to find a match. [S1] - **`case x:`** โ€” a candidate value to match against; its code block runs if the switch expression equals `x`. [S1] - **`break`** โ€” exits the switch block immediately once a matching case has been handled, preventing execution from continuing into subsequent cases. [S1] - **`default:`** โ€” optional; runs if no case matches; must be the LAST statement in the switch and does not need its own `break`. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Weekday-name lookup via switch: `int day = 4; switch (day) { case 1: printf("Monday"); break; ... case 4: printf("Thursday"); break; ... }` โ€” outputs "Thursday" for day 4. [S1] - Using default for unmatched cases: `int day = 4; switch (day) { case 6: printf("Today is Saturday"); break; case 7: printf("Today is Sunday"); break; default: printf("Looking forward to the Weekend"); }` โ€” day 4 matches neither case 6 nor 7, so default runs. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **break ์ƒ๋žต ์‹œ ๋‹ค์Œ case๋กœ ๊ณ„์† ์ง„ํ–‰๋จ**: switch๋Š” ์ผ์น˜ํ•˜๋Š” case๋ฅผ ์ฐพ์•„ ์ง„์ž…์ ๋งŒ ๊ฒฐ์ •ํ•  ๋ฟ, break๊ฐ€ ์—†์œผ๋ฉด ์ดํ›„ ๋ชจ๋“  case์˜ ์ฝ”๋“œ๊ฐ€ ๊ณ„์† ์‹คํ–‰๋œ๋‹ค๋Š” ์ (fall-through)์ด ์•”๋ฌต์ ์œผ๋กœ ๊ฒฝ๊ณ ๋จ. [S1] - **default๋Š” ๋ฐ˜๋“œ์‹œ ๋งˆ์ง€๋ง‰์— ์œ„์น˜**: default ํ‚ค์›Œ๋“œ๋Š” switch ๋ธ”๋ก์˜ ๋งˆ์ง€๋ง‰ statement์—ฌ์•ผ ํ•˜๋ฉฐ break๊ฐ€ ํ•„์š” ์—†๋‹ค๋Š” ์ ์ด ๋ช…์‹œ์ ์œผ๋กœ ํ™•์ธ๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ์š”์ผ ๋ฒˆํ˜ธ(1-7)๋ฅผ ์š”์ผ ์ด๋ฆ„์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์ œ์™€, ์ฃผ๋ง์ด ์•„๋‹Œ ์š”์ผ์— ๋Œ€ํ•ด default๋กœ "์ฃผ๋ง์ด ๊ธฐ๋Œ€๋œ๋‹ค"๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์˜ˆ์ œ๊ฐ€ ์›๋ฌธ์—์„œ ์ง์ ‘ ์‹ค์ „ ํ™œ์šฉ ์‚ฌ๋ก€๋กœ ์ œ์‹œ๋จ. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Using default to handle any value not matched by an explicit case (C): ```c int day = 4; switch (day) { case 6: printf("Today is Saturday"); break; case 7: printf("Today is Sunday"); break; default: printf("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.86 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[C Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[C Conditions RealLife]], [[C While Loop]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** switch ์„น์…˜ ์œ ์ผ ์ฑ•ํ„ฐ โ€” ๋ฐ˜๋ณต๋ฌธ(Loops) ์„น์…˜์˜ while ๋ฃจํ”„(While Loop) ์ฑ•ํ„ฐ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C Switch โ€” https://www.w3schools.com/c/c_switch.php ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Switch" page (Astra wiki-curation, P-Reinforce v3.1 format).