학습 목표

  • for 반복문과 while 반복문을 이해하고 사용할 수 있다.
  • 반복문의 종료 조건과 제어문(break, continue)을 활용할 수 있다.
  • 간단한 반복 작업 프로그램을 직접 만들 수 있다.

for문과 while문 기본 익히기

5.1 반복문이란?

  • 반복문은 같은 작업을 여러 번 자동으로 시키는 방법입니다.
  • 두 가지 방식:
    • for문: 정해진 횟수만큼 반복
    • while문: 조건이 참일 동안 반복

5.2 for 반복문 기본

for i in range(5):
    print("반복 중:", i)
  • range(5)는 0부터 4까지 5번 반복

실습 1: for문으로 숫자 출력

문제: 1부터 10까지 출력하는 코드를 for문으로 작성하세요.

모범답안:

for i in range(1, 11):
    print(i)

해설:

  • range(1, 11)은 1부터 10까지 포함합니다.

5.3 while 반복문 기본

count = 0
while count < 5:
    print("while 반복:", count)
    count += 1

실습 2: while로 숫자 출력

문제: 1부터 5까지 출력하는 while문을 작성하세요.

모범답안:

i = 1
while i <= 5:
    print(i)
    i += 1

해설:

  • 반복 조건이 거짓(False)이 되면 반복이 종료됩니다.

5.4 break와 continue

  • break: 반복을 즉시 종료
  • continue: 다음 반복으로 건너뜀
for i in range(10):
    if i == 5:
        break
    print(i)
for i in range(10):
    if i % 2 == 0:
        continue
    print(i)

실습 3: 제어문 활용

문제: 1부터 10까지 숫자 중 5에서 멈추고, 짝수는 건너뛰며 출력하세요.

모범답안:

for i in range(1, 11):
    if i == 5:
        break
    if i % 2 == 0:
        continue
    print(i)

해설:

  • if i == 5:에서 반복 종료
  • i % 2 == 0이면 건너뜀 (continue)

실전 반복문 활용

5.5 누적합 계산

실습 4: 1부터 10까지 합 계산

문제: 1~10까지 더한 합계를 출력하는 프로그램을 작성하세요.

모범답안:

total = 0
for i in range(1, 11):
    total += i
print("합계:", total)

해설:

  • += 연산자를 사용하여 계속 값을 더합니다.

5.6 구구단 출력

실습 5: 2단 출력

문제: for문을 사용하여 2단 구구단을 출력하세요.

모범답안:

for i in range(1, 10):
    print("2 x", i, "=", 2 * i)

해설:

  • 반복 변수 i를 이용해 곱셈 결과를 출력합니다.

5.7 별 출력하기

실습 6: 별 피라미드

문제: 아래와 같이 별을 출력하는 코드를 작성하세요.

*
**
***
****
*****

모범답안:

for i in range(1, 6):
    print("*" * i)

해설:

  • 문자열 곱셈을 활용하면 반복된 문자를 출력할 수 있습니다.

마무리 퀴즈 & 정리

  1. range(1, 4)의 출력 결과는? → 1, 2, 3
  2. while문은 어떤 조건에서 멈추나요? → 조건이 False가 되면 종료
  3. breakcontinue의 차이는?
    • break: 반복을 즉시 종료
    • continue: 다음 반복으로 건너뜀

다음 시간 예고

리스트와 튜플을 배워서 여러 데이터를 한 번에 저장하고 꺼내 쓰는 법을 알아볼 거예요!


TechTinkerer's에서 더 알아보기

구독을 신청하면 최신 게시물을 이메일로 받아볼 수 있습니다.

댓글 남기기

  • Mastering Python Data Science Essentials with Pandas, NumPy, and Matplotlib

    [Tutorial] · 2026-04-29 23:56 UTC Mastering Python Data Science Essentials with Pandas, NumPy, and Matplotlib 💡 TL;DR Get started with Python data science using Pandas, NumPy, and Matplotlib, covering data manipulation, numerical computations, and visualization techniques. 📚 Learning Objectives This tutorial covers the fundamental concepts of Python data science using popular libraries like Pandas for…

  • Mastering React Hooks for Efficient State Management and Context Access

    [Tutorial] · 2026-04-29 04:46 UTC Mastering React Hooks for Efficient State Management and Context Access 💡 TL;DR Discover the power of React Hooks for state management and context access, enabling you to write more efficient and scalable functional components. 📚 Learning Objectives Learn how to effectively use React Hooks to manage state and access context…

  • Building a Simple Game with C++: A Step-by-Step Guide

    [Tutorial] · 2026-04-29 03:43 UTC Building a Simple Game with C++: A Step-by-Step Guide 💡 TL;DR Learn how to create a simple game with C++ by setting up a game window, handling user input, and detecting collisions. 📚 Learning Objectives This tutorial covers the basics of creating a simple game using C++. You will learn…

  • Mastering Web Development Fundamentals: HTML, CSS, and JavaScript Basics

    Welcome to Web Development Fundamentals! This is a paragraph of text. Visit Example Website

  • Mastering Python List Comprehensions for Efficient Coding

    [Tutorial] · 2026-04-29 01:36 UTC Mastering Python List Comprehensions for Efficient Coding 💡 TL;DR Learn how to use Python’s powerful list comprehension feature to write efficient and readable code for creating lists and performing operations on them. 📚 Learning Objectives This tutorial covers the basics and advanced topics of Python list comprehensions, showcasing their power…

← 뒤로

응답해 주셔서 감사합니다. ✨

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기