SlideShare a Scribd company logo
☞
C++ 기초 프로그래밍
지난시간 복습
- 시험 점수를 입력받아 90 ~ 100점은 A,
- 80 ~ 89점은 B, 70 ~ 79점은 C, 60 ~ 69점은 D,
- 나머지 점수는 F를 출력하는 프로그램을 작성하시오.
1. Cin을 이용해서 시험점수를 입력받는다.
2. if문을 이용해서 프로그램을 제어한다
3. Cout을 이용해서 등급을 출력하여라
C++ 기초 프로그래밍
반복문(while)
- 프로그램을 반복시키기 위한 키워드
- while문 구조
o while(조건식){
Code...
}
C++ 기초 프로그래밍
반복문(while)예제
#include <iostream>
using namespace std;
int main() {
cout << 1 << endl;
cout << 2 << endl;
cout << 3 << endl;
cout << 4 << endl;
cout << 5 << endl;
cout << 6 << endl;
cout << 7 << endl;
cout << 8 << endl;
cout << 9 << endl;
cout << 10 << endl
}
C++ 기초 프로그래밍
반복문(while)예제
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 10) {
cout << i << endl;
i++;
}
cout << "terminate program" << endl;
}
C++ 기초 프로그래밍
반복문(while)예제2
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (true) {
cout << i << " ";
i++;
}
}
C++ 기초 프로그래밍
반복문(while)예제3
int main() {
// 2^0 = 1
// 2^1 = 2
// ...
// 2^6 = 64
int n;
cin >> n;
int i = 0, p = 1;
while (i <= n) {
cout << "2^" << i << "= " << p << endl;
i++;
p = p * 2;
}
}
C++ 기초 프로그래밍
반복문(do - while)예제
#include <iostream>
using namespace std;
int main() {
int i = 15;
do {
cout << i << endl;
i++;
} while (i <= 10);
cout << "terminate program" << endl;
}
C++ 기초 프로그래밍
반복문(do - while)예제2
#include <iostream>
using namespace std;
int main() {
int n;
do {
cout << "please enter 0" << endl;
cin >> n;
} while (n != 0);
cout << "thank you man" << endl;
}
C++ 기초 프로그래밍
반복문(for)
- 프로그램을 반복시키기 위한 키워드
- for문 구조
o for(반복을 위한 변수; 조건식; 증감식) {
Code...
}
C++ 기초 프로그래밍
반복문(for)예제
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i++) {
cout << i << endl;
}
}
C++ 기초 프로그래밍
반복문(for)예제2
#include <iostream>
using namespace std;
int main() {
// 1, 2, 4, 8, 16, 32, ....
int n;
cin >> n;
for (int i = 1; i <= n; i = i * 2) {
cout << i << endl;
}
}
C++ 기초 프로그래밍
반복문(for)예제3
#include <iostream>
using namespace std;
int main() {
// 1~n까지 숫자의 합을 출력
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n;i++){
sum = sum + i;
}
cout << sum << endl;
}
C++ 기초 프로그래밍
반복문(for)예제4
#include <iostream>
using namespace std;
int main() {
// *을 n개 출력
int n;
cin >> n;
for (int i = 1; i <= n;i++){
cout << "*" << endl;
}
}
C++ 기초 프로그래밍
반복문(for)예제5
int main() {
// 2^0 = 1
// 2^1 = 2
// ...
// 2^6 = 64
int n;
cin >> n;
for (int i = 0, p = 1; i <= n; i++, p = p * 2) {
cout << "2^" << i << "= " << p << endl;
p = p * 2;
}
}
C++ 기초 프로그래밍
반복문제어 (break, continue)
- break
반복문을 종료하기 위한 키워드
• continue
다음반복으로 넘어가기 위한 키워드
C++ 기초 프로그래밍
Break문 예제
int main() {
// break: 반복문 한개를 종료
// 숫자를 입력 한것을 그대로 출력한다
// 만약 유저가 -1을 입력하면 프로그램 종료
for (int i = 1; true; i++) {
int num;
cin >> num;
cout << i <<" 번째 숫자 " << num << endl;
if (num == -1) {
break;
}
}
}
C++ 기초 프로그래밍
Continue문 예제
int main() {
// 1부터 입력받은 수까지 중에서
// 3의배수를 제외하고 출력
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
if (i % 3 == 0) {
continue;
}
cout << i << endl;
}
}
C++ 기초 프로그래밍
중첩 for문 예제
int main() {
//중첩 for문
//정사각형을 출력하여라
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "*";
}
cout << endl;
}
}
C++ 기초 프로그래밍
If문 실습
- 첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째
줄에는 별 N개를 찍는 문제
- Ex) 5 입력 출력 *
**
***
****
*****

More Related Content

PPTX
RNC C++ lecture_2 Variable DataType
PPTX
RNC C++ lecture_2 operator, if
PPTX
불어오는 변화의 바람, From c++98 to c++11, 14
PDF
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
PDF
14장 - 15장 예외처리, 템플릿
PPTX
C언어 세미나 - 함수
PDF
6주차 스터디
PPTX
RNC C++ lecture_5 Array
RNC C++ lecture_2 Variable DataType
RNC C++ lecture_2 operator, if
불어오는 변화의 바람, From c++98 to c++11, 14
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
14장 - 15장 예외처리, 템플릿
C언어 세미나 - 함수
6주차 스터디
RNC C++ lecture_5 Array

What's hot (20)

PPTX
C++ 11 에 대해서 쉽게 알아봅시다 1부
PPTX
[KGC 2011]Boost 라이브러리와 C++11
PDF
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
PDF
Boost 라이브리와 C++11
PDF
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
PPTX
WTL 소개
PDF
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
PDF
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
PDF
2. c언어의 기본
PPTX
모던 C++ 정리
PPTX
20150212 c++11 features used in crow
PDF
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
PPTX
Nexon Developers Conference 2017 Functional Programming for better code - Mod...
PDF
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
PPTX
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
PDF
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
PDF
[C++ Korea 2nd Seminar] C++17 Key Features Summary
PPTX
[C++ korea] effective modern c++ study item8~10 정은식
PDF
5 1. character processing
PDF
3 1. preprocessor, math, stdlib
C++ 11 에 대해서 쉽게 알아봅시다 1부
[KGC 2011]Boost 라이브러리와 C++11
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
Boost 라이브리와 C++11
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
WTL 소개
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
2. c언어의 기본
모던 C++ 정리
20150212 c++11 features used in crow
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
Nexon Developers Conference 2017 Functional Programming for better code - Mod...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[NDC2015] C++11 고급 기능 - Crow에 사용된 기법 중심으로
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ korea] effective modern c++ study item8~10 정은식
5 1. character processing
3 1. preprocessor, math, stdlib
Ad

Similar to RNC C++ lecture_4 While, For (20)

PPTX
C review
PDF
03장 조건문반복문네임스페이스
PPTX
03장 조건문, 반복문, 네임스페이스
PDF
HI-ARC PS 101
PPTX
C언어 종결 세미나 1
PDF
[C언어] 반복문_for문
PPTX
반복문
PPT
C수업자료
PPT
C수업자료
PDF
C Language For Arduino
PDF
2015 Kitel C 언어 강좌2
PDF
C 언어 스터디 01 - 기초
PDF
[C언어] 반복문 (While)
PPTX
Processing 3rd Class: If & Loop
PPTX
Ch.14 파일 강c v0.6
PDF
7 mid term summary
PDF
2013 C++ Study For Students #1
PDF
코딩인카페 C&JAVA 기초과정 C프로그래밍(3)
PDF
4. loop
PPTX
[170327 1주차]C언어 A반
C review
03장 조건문반복문네임스페이스
03장 조건문, 반복문, 네임스페이스
HI-ARC PS 101
C언어 종결 세미나 1
[C언어] 반복문_for문
반복문
C수업자료
C수업자료
C Language For Arduino
2015 Kitel C 언어 강좌2
C 언어 스터디 01 - 기초
[C언어] 반복문 (While)
Processing 3rd Class: If & Loop
Ch.14 파일 강c v0.6
7 mid term summary
2013 C++ Study For Students #1
코딩인카페 C&JAVA 기초과정 C프로그래밍(3)
4. loop
[170327 1주차]C언어 A반
Ad

RNC C++ lecture_4 While, For