while + 기간 : -하는 동안

for + 숫자 : 딱딱 떨어지는 경우


#include <iostream>


int main()

{

using namespace std;


int count = 0;

for (; count < 10; ++count)

{

cout << count << endl;

}


cout << count << endl;


return 0;

}

<result>

1 -10까지 출력 

10이 출력되는 이유는 조건이 성립하지 않았지만
loop함수로 검증을 위해 돌아갔기 때문.


무한루프

for( ; ; ++count) or for( ; true; ++count)

while(true)


거듭제곱 함수 만들기 (pow 함수)

#include <iostream>


int pow(int base, int exponent)

{

int result = 1;


for (int count = 0; count < exponent; ++count)

result *= base;


return result;

}


int main()

{

using namespace std;


cout << pow(2, 4) << endl;


return 0;

}

 

count 2씩 빼기 

for (int count = 9; count >= 0; count -=2)

{

cout << count << endl;

}

*변수를 unsigned로 사용할 경우 오류가 나니 주의할 것. 


for문에 변수 2개 사용 

for (int i = 0, j = 0; i < 10; ++i, --j)

{

cout << i << " " << j << endl;

}


'programming > c++' 카테고리의 다른 글

5.9 난수 만들기  (0) 2019.05.04
5.8 break, continue  (0) 2019.05.04
5.6 do while  (0) 2019.04.30
5.5 반복문 while  (0) 2019.04.30
5.4 goto  (0) 2019.04.30
Posted by 도이(doi)
,