[C++]_S3-02_반복문

신치우·2024년 6월 22일

CPP

목록 보기
20/62
#include <iostream>
using namespace std;

// 반복문

int main()
{
	//while - 동안에
	// if - else 유용
	// 하지만 특정 조건까지 계속 반복해야하는 상황
	// ex) 게임을 끌때까지 계속 게임을 실행
	// ex) 목적지에 도달할때까지 -- 계속 이동

	int count = 0;

	/*while (count < 5)
	{
		cout << "Hello World" << endl;
		count++;
	}*/

	/*do
	{
		cout << "Hello World" << endl;
		count++;
	} while (false);*/

	// 초기화, 조건식, 제어식
	for (count = 0; count<5; count++)
	{
		cout << "Hello World" << endl;
	}

	// break;

	int round = 1;
	int hp = 100;
	int damage = 30;

	while (true)
	{
		hp -= damage;
		if (hp < 0)
			hp = 0; // 음수를 체력 0으로 보정

		// 시스템 메시지
		cout << "Round" << round << "몬스터 체력 " << hp << endl;

		if (hp == 0) {
			cout << "몬스터 처치!" << endl;
			break;
		}

		if (round == 5) {
			cout << "제한 라운드 종료" << endl;
			break;
		}
		round++;
	}

	// continue;
	// 1-10사이의 홀수만 뽑기
	for (count = 0; count <= 10; count++)
	{
		bool isEven = ((count % 2) == 0);
		if (isEven)
			continue;

		cout << count << endl;
	}
}

반복문의 종류
1. while (조건) {내용} -- 조건을 만족하면 내용을 반복한다
2. do{내용} while(조건) -- 먼저 1회 실행후 조건을 만족하면 다시 반복한다
3. for (초기화; 조건식; 제어식){내용} -- 값을 초기화후 조건을 만족하면 내용이 동작하고 제어식이 동작한다
4. break -- 반복문을 탈출
5. continue -- 다음 내용으로 반복한다

profile
https://shin8037.tistory.com/

0개의 댓글