--- id: c-do-while-loop title: "C Do/While Loop" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["do while", "at-least-once loop", "scanf input validation loop", "C do-while 반볡문"] 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", "loops", "do-while"] raw_sources: ["https://www.w3schools.com/c/c_do_while_loop.php"] applied_in: [] github_commit: "" --- # [[C Do/While Loop]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) The core difference from a regular `while` loop isn't cosmetic syntax β€” it's WHEN the condition is checked: `do/while` runs the body FIRST and evaluates the condition only AFTER, guaranteeing at least one execution even if the condition is false from the very start (`int i = 10; do {...} while (i < 5);` still runs once), which is exactly the opposite of the plain `while` loop's zero-iteration behavior shown in the previous chapter. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`do { ... } while (condition);`** β€” executes the block ONCE unconditionally, then checks the condition to decide whether to repeat. [S1] - **Guaranteed first execution** β€” unlike `while`, the body runs at least once even if the condition is false immediately. [S1] - **Use case: "at least once" behavior** β€” ideal for scenarios like showing a message or prompting for user input, where the action must happen before any condition can even be evaluated. [S1] - **Input-validation pattern** β€” repeatedly prompting via `scanf()` until the user provides a value that fails the loop condition (e.g. entering 0 or negative to stop). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Standard case (condition true at start): `int i = 0; do { printf("%d\n", i); i++; } while (i < 5);`. [S1] - Guaranteed-once case (condition false at start): `int i = 10; do { printf("i is %d\n", i); i++; } while (i < 5);` β€” still prints once despite `i < 5` being false immediately. [S1] - Practical input-validation loop: `int number; do { printf("Enter a positive number: "); scanf("%d", &number); } while (number > 0);` β€” keeps prompting until 0 or a negative number is entered. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **do/while은 쑰건과 λ¬΄κ΄€ν•˜κ²Œ μ΅œμ†Œ 1회 μ‹€ν–‰**: 일반 while λ£¨ν”„λŠ” 쑰건이 μ²˜μŒλΆ€ν„° 거짓이면 μ•„μ˜ˆ μ‹€ν–‰λ˜μ§€ μ•Šμ§€λ§Œ, do/while은 쑰건이 거짓이어도 λ°˜λ“œμ‹œ ν•œ λ²ˆμ€ μ‹€ν–‰λœλ‹€λŠ” 점이 직접 λŒ€λΉ„λ˜μ–΄ 강쑰됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) μ‚¬μš©μžλ‘œλΆ€ν„° μ–‘μˆ˜λ₯Ό μž…λ ₯λ°›λ˜ 0μ΄λ‚˜ μŒμˆ˜κ°€ μž…λ ₯될 λ•ŒκΉŒμ§€ λ°˜λ³΅ν•΄μ„œ λ¬Όμ–΄λ³΄λŠ” μž…λ ₯ 검증 루프가 μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) An input-validation loop that must prompt at least once before checking the exit condition (C): ```c int number; do { printf("Enter a positive number: "); scanf("%d", &number); } while (number > 0); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C While Loop RealLife]], [[C For Loop]], [[C User Input]] - **μ°Έμ‘° λ§₯락:** while λ£¨ν”„μ˜ λ³€ν˜• β€” for 루프(For Loop) μ±•ν„°λ‘œ 이어짐, μ‚¬μš©μž μž…λ ₯(User Input) 챕터와 μ—°κ²°. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Do/While Loop β€” https://www.w3schools.com/c/c_do_while_loop.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Do/While Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).