들어가기 앞서

바로 직전 포스트에서 입출력을 사용해봤으니 이제 백준 문제를 풀 수 있다고 생각했는데

문제를 풀려고 보니깐 반복문이랑 조건문을 사용할 줄 모르는 상태였습니다

이 얼마나 오만방자한 생각인가...!

그래서 이번엔 반복문을 간단히 연습해보는 코드를 만들어볼 생각입니다

흠 그 전에 C++ 프로젝트 생성하는 법 부터 배워야 할 것 같습니다... 임의로 whilepractice.cpp라는 파일을 생성했는데 실행을 할 수가 없군요... C++의 길을 멀고 험하다

매번 새 프로젝트를 생성할 수 밖에 없나...

 

완성코드

#include <iostream>

int main()
{
	std::cout << "while문\n";
	int i = 0;
	while (i < 5) {
		std::cout << i << "\n";
		i++;
	}
	std::cout << "for문\n";
	for (int j = 0; j < 5; j++) {
		std::cout << j << "\n";
	}
}

{} 중괄호 안에 코드가 작성 되어야 한다,

상수 값 1을 더하기 위해서 += 1 이 아니라 ++을 사용한다,

for문의 형태 외에는 크게 다른 점이 없습니다.

 

정리

참고 사이트:

https://www.w3schools.com/cpp/cpp_while_loop.asp

 

C++ While Loop

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

https://www.w3schools.com/cpp/cpp_for_loop.asp

 

C++ For Loop

W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

www.w3schools.com

 

반응형

+ Recent posts