학습 목표

  • 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에서 더 알아보기

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

댓글 남기기

  • Build Your First Website from Scratch using HTML & CSS

    Hello, World! This is my first website.

  • Building a Simple Web Page: HTML & CSS Fundamentals

    Welcome to My Website This is a basic web page example.

  • Introduction to Data Science Concepts in C++

    [Tutorial] · 2026-01-13 23:41 UTC Introduction to Data Science Concepts in C++ 💡 TL;DR Explore fundamental data science concepts like arrays, strings, and basic math functions in C++, opening doors to practical data manipulation tasks. 📚 Learning Objectives This tutorial provides an introduction to key data science concepts in C++, including arrays, strings, and mathematical…

  • Building a Simple Game in C++: Beginner’s Guide to Object Oriented Programming and Graphic Design

    [Tutorial] · 2026-01-13 23:20 UTC Building a Simple Game in C++: Beginner’s Guide to Object Oriented Programming and Graphic Design 💡 TL;DR Learn how to create basic games using C++ by exploring object-oriented programming and drawing on the screen! 📚 Learning Objectives This tutorial will introduce beginners to game development using C++, covering basic programming…

  • Unveiling C++’s Power for Data Science

    [Tutorial] · 2026-01-13 08:25 UTC Unveiling C++’s Power for Data Science 💡 TL;DR Discover how to manipulate data efficiently using C++’s array, string, and math features for impactful data-driven solutions. 📚 Learning Objectives This tutorial introduces fundamental C++ concepts crucial for data science applications, focusing on arrays, strings, and mathematical functions. 🎯 Key Concepts Arrays…

← 뒤로

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

TechTinkerer's에서 더 알아보기

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

계속 읽기

TechTinkerer's에서 더 알아보기

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

계속 읽기