Notice
Recent Posts
Recent Comments
Link
오븐 노트
[C++] 반복문 본문
반복문 종류 및 사용법
#include <iostream>
using namespace std;
int main()
{
// while 문
int count1 = 0;
while (count1 < 5) // 조건이 참일 경우 통과
{
cout << "count1 : " << count1 << endl;
count1++;
}
// do while 문
int count2 = 0;
do // 한번은 무조건 통과
{
cout << "count2 : " << count2 << endl;
count2++;
} while (count2 < 5);
// for 문
for (int count3 = 0; count3 < 5; count3++) {
cout << "count3 : " << count3 << endl;
}
}
흐름 제어 (break 반복문 탈출)
#include <iostream>
using namespace std;
int main()
{
// 흐름 제어
int round = 1;
int hp = 100;
int damage = 27;
// 무한 루프 : 전투 시작
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 반복문 넘기기)
#include <iostream>
using namespace std;
int main()
{
// 1 ~ 10 사이 홀수 출력
for (int count = 0; count < 10; count++)
{
bool isEven = (count % 2) == 0;
/*if(!isEven) // 기존 if문 분기
cout << count << endl;*/
if (isEven) // 흐름제어 continue
continue;
cout << count << endl;
}
}
[C++과 언리얼로 만드는 MMORPG 게임 개발 시리즈] Part1: C++ 프로그래밍 입문 - 인프런 | 강의
C++ 카테고리의 글은 인프런 Rookiss님의 강의를 공부하며 정리하는 내용입니다.
이미 알고 있는 내용도 다시 정리 되어있을 수 있습니다.
모든 글은 제가 공부하기 위해 작성합니다.
'Develop > C++' 카테고리의 다른 글
[C++] 가위 바위 보 (0) | 2022.06.12 |
---|---|
[C++] 별찍기와 구구단 (0) | 2022.06.12 |
[C++] 분기문 (0) | 2022.05.31 |
[C++] 유의사항 (0) | 2022.05.23 |
[C++] const와 메모리 구조 (0) | 2022.05.16 |