학습 목표

  • 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 the Fundamentals of Object-Oriented Programming in C++

    [Tutorial] · 2026-01-15 04:15 UTC Mastering the Fundamentals of Object-Oriented Programming in C++ 💡 TL;DR Learn how to structure your code with classes, define objects, encapsulate data, and leverage inheritance for efficient development. 📚 Learning Objectives This tutorial introduces object-oriented programming concepts in C++, focusing on classes, objects, encapsulation, and inheritance. You’ll gain practical skills…

  • Building Your First Web App with Flask: A Simple To-Do List Example

    [Tutorial] · 2026-01-15 03:11 UTC Building Your First Web App with Flask: A Simple To-Do List Example 💡 TL;DR Learn how to create a simple web app with Flask by building a “To-Do List” example using code examples. 📚 Learning Objectives This tutorial teaches beginners how to build a basic web application using Flask, a…

  • Mastering Python Data Structures: Lists, Tuples, and Dictionaries

    [Tutorial] · 2026-01-15 02:08 UTC Mastering Python Data Structures: Lists, Tuples, and Dictionaries 💡 TL;DR Learn how to use lists, tuples, and dictionaries in Python to store, organize, and access data effectively. 📚 Learning Objectives This tutorial introduces essential Python data structures (lists, tuples, dictionaries) for organizing and manipulating data efficiently. It provides hands-on examples…

  • Mastering Python’s List Comprehensions for Efficient Code Writing

    [Tutorial] · 2026-01-15 01:06 UTC Mastering Python’s List Comprehensions for Efficient Code Writing 💡 TL;DR Learn how list comprehensions streamline your Python code by creating lists automatically from iterables. 📚 Learning Objectives This tutorial will introduce you to list comprehensions in Python, empowering you to create concise code that builds lists based on existing iterables.…

  • Mastering Web Design Basics: HTML & CSS for Beginners

    Welcome to my website! This is a simple introductory paragraph.

← 뒤로

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

TechTinkerer's에서 더 알아보기

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

계속 읽기

TechTinkerer's에서 더 알아보기

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

계속 읽기