C++ 공부 (4/21)

wsung·2026년 1월 21일

Day 4 — 반복문 (for / while)

🎯 오늘 목표
반복문이 왜 필요한지 체감
for / while 차이 명확히 이해
반복 횟수·조건을 내가 통제

for문

  • 기본구조
    for(시작; 조건;변화)
    {
    실행코드
    }
#include <iostream>

int main()
{
	for(int i=1; i<=5; i++)
    {
    	std:cout<< i << std::endl;
	}
    return 0;
}

while (조건 중심)

기본구조
while (조건) {
실행;
}
=> ~하는 동안 계속

#include <iostream>

int main()
{
	int i = 1;
    
    while( i<=5)
    {
    	std::cout << i << std::endl;
		i++;
	}
    
    return 0;
}

for/while 언제??

  1. 반복 횟수가 정해질 때 -> for
  2. 조건이 만족할 때까지 -> while

break / countinue (중요제어)

  • break : 즉시 반복 종료
for (int i =1; i<=10; i++)
{
	if(i==5)
    	break;

	std::cout<< i << std::endl;
}

=> 1 2 3 4
  • countinue ( 이번 반복만 건너 뛰기)
for(int i =1; i<=5; i++)
{
	if(i ==3 )
    	countinue;

	std::cout << i << std::endl;
}

=> 1 2 4 5
profile
0부터 시작하는 백엔드

0개의 댓글