'programming/c++'에 해당되는 글 52건

  1. 2019.05.04 5.7 반복문 for
  2. 2019.04.30 5.6 do while
  3. 2019.04.30 5.5 반복문 while
  4. 2019.04.30 5.4 goto

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)
,

5.6 do while

programming/c++ 2019. 4. 30. 12:10

while은 조건에 따라서 실행되지 않을 수도 있지만,

do while은 조건에 상관없이 반드시 한 번 실행된다. 


반드시 한 번 실행되는 것을 강조할 때 사용됨. 


사용자가 입력한 숫자가 1, 2, 3, 4가 아닐 경우 다시 입력하라는 프로그램

int selection; //do-while밖에 있어야 됨. 

do 

{

cout << "1. add" << endl;

cout << "2. sub" << endl;

cout << "3. mult" << endl;

cout << "4. div" << endl;

cin >> selection;

} while (selection <= 0 || selection >= 5); //do-while 다음에 ';' 찍어야 됨 

cout << "You selected" << selection << endl;


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

5.8 break, continue  (0) 2019.05.04
5.7 반복문 for  (0) 2019.05.04
5.5 반복문 while  (0) 2019.04.30
5.4 goto  (0) 2019.04.30
5.3 switch case  (0) 2019.04.30
Posted by 도이(doi)
,

int count = 0; //counter선언

while ( count < 10 ) //while 조건이 true여야 출력된다. 

{

cout << count << endl;

++count;


if( count == 10 ) break;

}


몇 번에 한번씩 어떤 행위를 하도록 출력하고 싶다. 

int count = 1;

while( count < 100)

{

if( count%5 == 0 ) cout << "Hello" << count << endl;

count ++;


while문 안에 while문 

int outer_count = 1;


while (outer_count <= 5)

{

int inner_count =1;

while(inner_count <= outer_count)

{

cout << inner_count++ << " ";

}


cout << endl;

++outer_count;


<Result>

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

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

5.7 반복문 for  (0) 2019.05.04
5.6 do while  (0) 2019.04.30
5.4 goto  (0) 2019.04.30
5.3 switch case  (0) 2019.04.30
5.1 제어 흐름 소개  (0) 2019.04.30
Posted by 도이(doi)
,

5.4 goto

programming/c++ 2019. 4. 30. 11:41

double x;

tryAgain : // label

cout << "Enter a non-negative number" << endl;

cin >> x;


if(x <0.0)

goto tryAgain; //tryAgain(label)로 돌아가라 


cout << sqrt(x) << endl;


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

5.6 do while  (0) 2019.04.30
5.5 반복문 while  (0) 2019.04.30
5.3 switch case  (0) 2019.04.30
5.1 제어 흐름 소개  (0) 2019.04.30
4.10 구조체  (0) 2019.04.29
Posted by 도이(doi)
,