#include <iostream> using namespace std;
int main() { unsigned int counter{ 1 };
while (counter <= 10) { cout << counter << " "; ++counter; }
cout << endl; } |
위의 예제는 1부터 10까지의 정수를 while 반복문을 이용하여 출력한다.
#include <iostream> using namespace std;
int main() { for (unsigned int counter{ 1 }; counter <= 10; ++counter) cout << counter << " ";
cout << endl; } |
위의 예제는 동일한 결과를 while이 아니라 for 문을 이용하여 출력한다.
for문은 (초기화; 반복 지속 조건; 증감)으로 구성된다.