1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
 
// 반복문
// 데이터를 메모리에 할당하고 가공하고 방법에 대해 알아봄
// 가공한 데이터를 이용해서 무엇인가를 하고 싶다면?
 
int main()
{
 
#pragma region while
    // 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 (int count = 0; count < 5; count++)
    {
        cout << "Hello World" << endl;
    }
 
 
 
    // break; continue;
    while (조건식1)
    {
        while (조건식2)
        {
            // 반복문에서 빠져나가주세요
            break;
 
            명령문
        }
    }
 
    // 루프문의 흐름 제어 break continue
#pragma endregion
 
 
    int round = 1;
    int hp = 100;
    int damage = 10;
 
    // 무한 루프 : 전투 시작
    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++;
    }
 
    // 1~10 사이의 홀수만 출력하기
 
    // STYLE 1
    for (int count = 1; count <= 10; count++)
    {
        bool isEven = ((count % 2== 0);
        if (isEven == false)
        {
            cout << count << endl;
        }
    }
    
    // STYLE 2
    for (int count = 1; count <= 10; count++)
    {
        bool isEven = ((count % 2== 0);
        if (isEven)
            continue;      // 짝수 부분 스킵해서 지나감
 
        cout << count << endl// 홀수 부분 출력
        
    }
 
}
cs