--- id: cpp-for-loop title: "C++ For Loop" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["for statement", "three-statement loop", "C++ for 반볡문"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.84 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["cpp", "programming-language", "w3schools", "loops", "for-loop"] raw_sources: ["https://www.w3schools.com/cpp/cpp_for_loop.asp"] applied_in: [] github_commit: "" --- # [[CPP For Loop]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) C++'s `for` loop declares its counter INLINE inside the loop header (`for (int i = 0; i < 5; i++)`) rather than declaring `int i;` separately beforehand β€” a syntax choice the source demonstrates consistently across every example, meaning `i`'s SCOPE is confined to the loop itself in idiomatic C++ usage, unlike C's tutorial style which more often declared the counter as a separate statement before the loop. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`for (statement1; statement2; statement3) { ... }`** β€” statement1 runs once before the loop; statement2 is the continuation condition; statement3 runs after each iteration. [S1] - **Inline counter declaration** β€” `for (int i = 0; ...)` scopes `i` to the loop itself, the idiomatic C++ style shown throughout. [S1] - **Fixed-count use case** β€” the recommended tool when the number of iterations is known ahead of time. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic counting with an inline-declared counter: `for (int i = 0; i < 5; i++) { cout << i << "\n"; }`. [S1] - Accumulating a sum: `int sum = 0; for (int i = 1; i <= 5; i++) { sum = sum + i; } cout << "Sum is " << sum;`. [S1] - Countdown via decrement: `for (int i = 5; i > 0; i--) { cout << i << "\n"; }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **루프 μΉ΄μš΄ν„°λ₯Ό for 헀더 μ•ˆμ—μ„œ λ°”λ‘œ μ„ μ–Έ**: C μ±•ν„°μ˜ μ˜ˆμ œλ“€μ€ int i;λ₯Ό 루프 λ°–μ—μ„œ λ³„λ„λ‘œ μ„ μ–Έν•˜λŠ” κ²½μš°κ°€ μžˆμ—ˆμ§€λ§Œ, C++ μ˜ˆμ œλŠ” μΌκ΄€λ˜κ²Œ for (int i = 0; ...) ν˜•νƒœλ‘œ 헀더 μ•ˆμ—μ„œ λ°”λ‘œ μ„ μ–Έν•΄ i의 μŠ€μ½”ν”„λ₯Ό 루프에 κ΅­ν•œμ‹œν‚¨λ‹€λŠ” 점이 확인됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 1λΆ€ν„° 5κΉŒμ§€μ˜ 합을 for λ£¨ν”„λ‘œ λˆ„μ  κ³„μ‚°ν•˜λŠ” 예제(sum = sum + i)κ°€ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) An inline-declared loop counter scoped to the for loop itself (C++): ```cpp int sum = 0; for (int i = 1; i <= 5; i++) { sum = sum + i; } cout << "Sum is " << sum; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.84 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C++ Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CPP Do While Loop]], [[CPP For Loop Foreach]], [[C For Loop]] - **μ°Έμ‘° λ§₯락:** for 루프 β€” foreach 루프(For Loop Foreach) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C++ For Loop β€” https://www.w3schools.com/cpp/cpp_for_loop.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C++ For Loop" page (Astra wiki-curation, P-Reinforce v3.1 format).