독개

[C++] 5.5 반복문 while

by #독개#

/*
조건이 참이면 { }안을 계속 루프시킴
break 만나면 빠져나옴
while(조건)
{

}
while(1) 이런식으로 무한루프로도 많이씀 if와 같이 0빼곤 다 참임
while에 static 변수 사용하는경우 있다
while에 최적화를 위해서 -로 가지않는경우 unsigned를 쓰기도한다
while문안에 while문을 넣을수있다
*/

#include <iostream>

using namespace std;

int main()
{
	int count = 0;
	while (1)
	{
		cout << count << endl;
		++count;
	}

	while (1)
	{
		static int count2 = 0;
		cout << count2 << endl;
		++count2;
	}

	//위 두개가 같다 종종 static으로도 사용하게 될거다

///----------------------------------------------------------------------

	//5번에 한번씩 출력하고싶을때
	int count5 = 1;
		while (count5 < 20);
	{
			if (count % 5 == 0) cout << "Hello " << count5 << endl;
	}

///---------------------------------------------------------------------- 

	//실전에서 문제가 될수 있는 부분
	//5 4 3 2 1 가비지값 가비지값 가비지값 나옴
	//증가하는 경우에는 signed안쓰고 unsigned써도 되겠지 unsigned가 더빠르다
	//최적화해야하는 프로그램은 진짜 쥐어짜므로 이런것조차 다바꿔줘야한다
	//최적화를 위해선 unsigned를 쓰는게 좋다
	unsigned int count3 = 5;
	while (true)
	{
		cout << count3 << 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;
	}

	return 0;


}

 

 

 

 


🐱‍👓독하게 개발

'🔥 프로그래밍 학습 > C++' 카테고리의 다른 글

[C++] 5.7 반복문 for  (0) 2022.11.14
[C++] 5.5 반복문 while  (1) 2022.11.14
[C++] 5.4 goto  (0) 2022.11.13
[C++] 5.3 Switch-Case  (0) 2022.11.13
[C++] 5.1 IF 조건문 (주의사항위주)  (0) 2022.11.13

블로그의 정보

독한 개발자

#독개#

활동하기