SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
Loop: while, do-while, for
차례
 반복명령문:
– while 문
– for 문
– do-while
 제어이동 명령문:
– break, continue,
 예제
2
While 문
 While 문 type 1
3
조건
F
T
B
A C
while( 조건 )
{
A ;
B ;
}
C ;
B
A
C
조건
While 문
 While 문 type 2
4
조건
F
T
B C
while( 1 )
{
A ;
if( !조건 ) break ;
B ;
}
C ;
A
While 문
 While 문 type 2
– Type1로 변형
5
조건
F
T
B C
A
조건
F
T
B C
A
A
A ;
while( 조건 )
{
B ;
A ;
}
C ;
While 문
 While 문 type 3
6
F
T
C
A
조건
B
while( 1 )
{
A ;
B ;
if( !조건 ) break ;
}
C ;
do
{
A ;
B ;
} while( 조건) ;
C ;
While 문
 While 문 type 3
– Type 1으로 변경
7
F
T
C
A
조건
B 조건
F
T
A C
A
B
B A ;
B ;
while( 조건 )
{
A ;
B ;
}
C ;
 주의 사항
while문
while (1) i++; /* infinite loop */
while(5/3) i++;
while(-1) i++;
while(0) i++; /*naver runs*/
8
for 문
 특수형태의 while문
9
조건
F
T
B E
A
D
for( A ; 조건 ; D )
{
B ;
C ;
}
E ;
C
A: 조건 바로 윗 문장
D: 반복되는 부분의 맨 끝 문장
for 문
 1부터 100사이의 짝수 더하기
10
Start
Var i, sum
Stop
i  100
sum
F
T
sum <- 0
i <- 2
i <- i + 2
sum <- sum + i
int i, sum ;
sum = 0 ;
i = 2 ;
while( i <= 100 ) {
sum = sum + i ;
i =+ 2 ;
}
printf( “%dn”, sum ) ;
int i, sum ;
sum = 0 ;
for( i = 2 ; i <= 100 ; i =+ 2) {
sum = sum + i ;
}
printf( “%dn”, sum ) ;
11
for 문
 for statement Syntax
for ( expr1; expr2; expr3 )
{
statement ;
...
}
next statement ;
expr1;
while (expr2)
{
statement ;
...
expr3;
}
next statement ;
for 문
 Read 10 numbers and sum up those
int i, n ;
sum= 0 ;
i= 0 ;
while ( i< 10) {
scanf( “%d”, &n ) ;
sum += n ;
i++ ;
}
printf( “%dn”, sum ) ;
12
int i, n ;
sum = 0 ;
for( i = 0 ; i < 10 ; i++ )
{
scanf( “%d”, &n ) ;
sum += n ;
}
printf( “%dn”, sum ) ;
for 문
 Read 10 numbers and sum up those
int i, n ;
sum= 0 ;
i = 0 ;
while ( i< 10) {
scanf( “%d”, &n ) ;
sum += n ;
i++ ;
}
printf( “%dn”, sum ) ;
13
int i, n ;
i= 0 ;
for( sum = 0 ; i < 10 ; sum+=n)
{
scanf( “%d”, &n ) ;
i++ ;
}
printf( “%dn”, sum ) ;
int i, n ;
i= 0 ;
sum = 0 ;
while ( i< 10) {
scanf( “%d”, &n ) ;
i++ ;
sum += n ;
}
printf( “%dn”, sum ) ;
int i, n ;
sum = 0 ;
for( i = 0 ; i < 10 ; i++ )
{
scanf( “%d”, &n ) ;
sum += n ;
}
printf( “%dn”, sum ) ;
14
for 문
 Counting up from 0 to n-1
 Counting up from 1 to n
 Counting up from n-1 to 0
 Counting up from n to 1
for ( i = 0; i < n; i++ ) A;
for ( i = 1; i <= n; i++ ) A;
for ( i = n -1; i >= 0; i-- ) A;
for ( i = n; i > 0; i-- ) A;
break 문
 break
– break문을 감싸는 루프에서 빠져 나온다.
15
while(A) {
B;
while(C) {
D;
if( E ) break;
F;
}
G;
if( H ) break ;
I ;
}
J;
continue 문
 continue
– continue를 감싸는 루프의 시작점(조건확인)으로 올라간다.
16
while(A) {
B;
while(C) {
D;
if( E ) continue;
F;
}
G;
if( H ) continue ;
I;
}
J;
continue 문
 continue
17
while(A) {
B;
while(C) {
D;
if( E ) continue;
F;
G;
}
H;
if( I ) continue ;
J;
K;
}
L;
while(A) {
B;
while(C) {
D;
if( !E ) {
F;
G;
}
}
H;
if( !I ) {
J;
K;
}
}
L;
 합이 7이 되는 양의 정수 3개의 리스트를 출력하시오.
Example
#include <stdio.h>
int main(void)
{
int i, j, k ;
for ( i = 0; i <= 7; ++i )
{
for( j = 0; j <= 7; ++j )
{
for ( k = 0; k <= 7; ++k )
{
if ( i + j + k == 7 )
printf( "%d %d %dn", i, j, k );
}
}
}
return 0;
}
0 0 7
0 1 6
0 2 5
0 3 4
…
5 0 2
5 1 1
…
6 1 0
7 0 0
18
Example 1
 Add all integers between 1 and 100
19
Start
Var i, sum
Stop
i  100
sum
F
T
i <- 1
sum <- 0
i <- i + 1
sum <- sum + i
#include <stdio.h>
int main() {
int i, sum ;
i = 0 ;
sum = 0 ;
while( i <= 100 )
{
sum = sum + i ;
i = i + 1 ;
}
printf( “%d”, sum ) ;
return 0;
}
Example 1-1
 Add all integers between 1 and 100
20
Start
Var i, sum
Stop
sum
i <- 1
sum <- 0
i <- i + 1
sum <- sum + i
#include <stdio.h>
int main() {
int i, sum ;
i = 0 ;
sum = 0 ;
while( i <= 100 )
{
sum = sum + i ;
i = i + 1 ;
}
printf( “%d”, sum ) ;
return 0;
}
i  100
Example 2
 Add all even integers between 1 and 100
21
Start
Var i, sum
Stop
sum
i <- 1, sum <- 0
i <- i + 1
sum <- sum + i
i is even ?
T
F
#include <stdio.h>
int main() {
int i, sum ;
i = 0 ;
sum = 0 ;
while( i <= 100 )
{
if( i % 2 == 0 )
{
sum = sum + i ;
}
i = i + 1 ;
}
printf( “%d”, sum ) ;
return 0;
}
i  100
Example 3
 Find the largest n such that 1+2+…+n<1000
22
Start
Var n, sum
Stop
sum < 1000
n-1F
T
n <- 1, sum <- 0
n <- n + 1
sum <- sum + n
#include <stdio.h>
#include <math.h>
int main() {
int n, sum ;
n = 1 ;
sum = 0 ;
while( 1)
{
sum = sum + n ;
if( sum < 1000) break ;
n = n + 1 ;
}
printf( “%d”, n-2 ) ;
return 0;
}
Example 3-1
 Find the largest n such that 1+2+…+n<1000
23
#include <stdio.h>
#include <math.h>
int main() {
int n, sum ;
n = 1 ;
sum = 0 ;
sum = sum + n ;
while( sum < 1000 )
{
n = n + 1 ;
sum = sum + n ;
}
printf( “%d”, n-2 ) ;
return 0;
}
Start
Var n, sum
Stop
n-1
n <- 1, sum <- 0
n <- n + 1
sum <- sum + n
sum <- sum + n
sum < 1000
Example 4
 (1+1+…+1)+ … + (10+10+…+10)
24
Start
Var a, b, sum
a = 1, sum = 0
sum
b = b + 1
sum = sum+ a
Stop
#include <stdio.h>
#include <math.h>
int main()
{
int a, b;
a = 1 ;
while( a <= 10 )
{
b = 1 ;
while( b <= 10 )
{
sum = sum + a ;
b = b + 1 ;
}
a = a + 1 ;
}
return 0;
}
a  10
b  10
b <- 1
a = a + 1
Example 5
 (2*1+2*2+…+2*9)+ … + (9*1+9*2+…+9*9)
25
Start
Var a, b, sum
a = 2, sum = 0
sum
b = b + 1
sum = sum+ a * b
Stop
#include <stdio.h>
#include <math.h>
int main()
{
int a, b;
a = 2 ;
while( a <= 9 )
{
b = 1 ;
while( b <= 9 )
{
sum = sum + a*b ;
b = b + 1 ;
}
a = a + 1 ;
}
return 0;
}
a  9
b  9
b <- 1
a = a + 1
Example 6
 1+(1+2)+(1+2+3)+......+(1+....+10)
26
Start
Var n, a,
sum
Stop
sum
n <- 1, sum <- 0
n <- n + 1
sum <- sum + a
#include <stdio.h>
#include <math.h>
int main()
{
int n, a, sum ;
n = 1 ;
a = 1 ;
sum = 0 ;
while( n <= 10 )
{
a = 1 ;
while( a <= n )
{
sum = sum + a ;
a = a + 1 ;
}
n = n + 1 ;
}
printf( “%d”, sum ) ;
return 0;
}
n  10
a <- 1
a  n
a <- a + 1

Más contenido relacionado

La actualidad más candente

Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3ssuser7c5a40
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차Yeonah Ki
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차Yeonah Ki
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차Yeonah Ki
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성Lee Sang-Ho
 
[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handlingSeok-joon Yun
 
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성Lee Sang-Ho
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기Chris Ohk
 

La actualidad más candente (8)

Modern effective c++ 항목 3
Modern effective c++ 항목 3Modern effective c++ 항목 3
Modern effective c++ 항목 3
 
게임프로그래밍입문 5주차
게임프로그래밍입문 5주차게임프로그래밍입문 5주차
게임프로그래밍입문 5주차
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
 
[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling[KOSSA] C++ Programming - 13th Study - exception handling
[KOSSA] C++ Programming - 13th Study - exception handling
 
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C++ 프로그래밍 과제물 작성
 
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
[TechDays Korea 2015] 녹슨 C++ 코드에 모던 C++로 기름칠하기
 

Destacado

03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스웅식 전
 
9 object class
9 object class9 object class
9 object class웅식 전
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11 웅식 전
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 

Destacado (17)

03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스
 
9 object class
9 object class9 object class
9 object class
 
13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
13. structure
13. structure13. structure
13. structure
 
15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
10th
10th10th
10th
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 

Similar a 4. loop

C언어 연산자에 대해 간과한 것
C언어 연산자에 대해 간과한 것C언어 연산자에 대해 간과한 것
C언어 연산자에 대해 간과한 것jaypi Ko
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, Foritlockit
 
이산치7보고서
이산치7보고서이산치7보고서
이산치7보고서KimChangHoen
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
C수업자료
C수업자료C수업자료
C수업자료koominsu
 
과제 1,2,3
과제 1,2,3과제 1,2,3
과제 1,2,3mil23
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
반복문
반복문반복문
반복문Saebyeol Ju
 
G+ Summer C Study 20130709(3일차)
G+ Summer C Study 20130709(3일차)G+ Summer C Study 20130709(3일차)
G+ Summer C Study 20130709(3일차)Jake Yoon
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿유석 남
 
이산수학 C1 프로젝트 5
이산수학 C1 프로젝트 5이산수학 C1 프로젝트 5
이산수학 C1 프로젝트 5pkok15
 
5통신망에서 길 찾기
5통신망에서 길 찾기5통신망에서 길 찾기
5통신망에서 길 찾기herojoon1378
 
Project#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpProject#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpKimjeongmoo
 
2012 Dm C2 03
2012 Dm C2 032012 Dm C2 03
2012 Dm C2 03seonhyung
 
자료구조 Project1
자료구조 Project1자료구조 Project1
자료구조 Project1KoChungWook
 

Similar a 4. loop (20)

C언어 연산자에 대해 간과한 것
C언어 연산자에 대해 간과한 것C언어 연산자에 대해 간과한 것
C언어 연산자에 대해 간과한 것
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
RNC C++ lecture_4 While, For
RNC C++ lecture_4 While, ForRNC C++ lecture_4 While, For
RNC C++ lecture_4 While, For
 
이산치1번
이산치1번이산치1번
이산치1번
 
이산치7보고서
이산치7보고서이산치7보고서
이산치7보고서
 
C수업자료
C수업자료C수업자료
C수업자료
 
C수업자료
C수업자료C수업자료
C수업자료
 
과제 1,2,3
과제 1,2,3과제 1,2,3
과제 1,2,3
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
HI-ARC PS 101
HI-ARC PS 101HI-ARC PS 101
HI-ARC PS 101
 
C review
C  reviewC  review
C review
 
반복문
반복문반복문
반복문
 
G+ Summer C Study 20130709(3일차)
G+ Summer C Study 20130709(3일차)G+ Summer C Study 20130709(3일차)
G+ Summer C Study 20130709(3일차)
 
14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿14장 - 15장 예외처리, 템플릿
14장 - 15장 예외처리, 템플릿
 
이산수학 C1 프로젝트 5
이산수학 C1 프로젝트 5이산수학 C1 프로젝트 5
이산수학 C1 프로젝트 5
 
이산수학05
이산수학05이산수학05
이산수학05
 
5통신망에서 길 찾기
5통신망에서 길 찾기5통신망에서 길 찾기
5통신망에서 길 찾기
 
Project#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 HwpProject#5 통신망에서 길 찾기 Hwp
Project#5 통신망에서 길 찾기 Hwp
 
2012 Dm C2 03
2012 Dm C2 032012 Dm C2 03
2012 Dm C2 03
 
자료구조 Project1
자료구조 Project1자료구조 Project1
자료구조 Project1
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스웅식 전
 
Goorm ide open beta
Goorm ide open betaGoorm ide open beta
Goorm ide open beta웅식 전
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary웅식 전
 

Más de 웅식 전 (16)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
 
Goorm ide open beta
Goorm ide open betaGoorm ide open beta
Goorm ide open beta
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
 

4. loop

  • 2. 차례  반복명령문: – while 문 – for 문 – do-while  제어이동 명령문: – break, continue,  예제 2
  • 3. While 문  While 문 type 1 3 조건 F T B A C while( 조건 ) { A ; B ; } C ; B A C 조건
  • 4. While 문  While 문 type 2 4 조건 F T B C while( 1 ) { A ; if( !조건 ) break ; B ; } C ; A
  • 5. While 문  While 문 type 2 – Type1로 변형 5 조건 F T B C A 조건 F T B C A A A ; while( 조건 ) { B ; A ; } C ;
  • 6. While 문  While 문 type 3 6 F T C A 조건 B while( 1 ) { A ; B ; if( !조건 ) break ; } C ; do { A ; B ; } while( 조건) ; C ;
  • 7. While 문  While 문 type 3 – Type 1으로 변경 7 F T C A 조건 B 조건 F T A C A B B A ; B ; while( 조건 ) { A ; B ; } C ;
  • 8.  주의 사항 while문 while (1) i++; /* infinite loop */ while(5/3) i++; while(-1) i++; while(0) i++; /*naver runs*/ 8
  • 9. for 문  특수형태의 while문 9 조건 F T B E A D for( A ; 조건 ; D ) { B ; C ; } E ; C A: 조건 바로 윗 문장 D: 반복되는 부분의 맨 끝 문장
  • 10. for 문  1부터 100사이의 짝수 더하기 10 Start Var i, sum Stop i  100 sum F T sum <- 0 i <- 2 i <- i + 2 sum <- sum + i int i, sum ; sum = 0 ; i = 2 ; while( i <= 100 ) { sum = sum + i ; i =+ 2 ; } printf( “%dn”, sum ) ; int i, sum ; sum = 0 ; for( i = 2 ; i <= 100 ; i =+ 2) { sum = sum + i ; } printf( “%dn”, sum ) ;
  • 11. 11 for 문  for statement Syntax for ( expr1; expr2; expr3 ) { statement ; ... } next statement ; expr1; while (expr2) { statement ; ... expr3; } next statement ;
  • 12. for 문  Read 10 numbers and sum up those int i, n ; sum= 0 ; i= 0 ; while ( i< 10) { scanf( “%d”, &n ) ; sum += n ; i++ ; } printf( “%dn”, sum ) ; 12 int i, n ; sum = 0 ; for( i = 0 ; i < 10 ; i++ ) { scanf( “%d”, &n ) ; sum += n ; } printf( “%dn”, sum ) ;
  • 13. for 문  Read 10 numbers and sum up those int i, n ; sum= 0 ; i = 0 ; while ( i< 10) { scanf( “%d”, &n ) ; sum += n ; i++ ; } printf( “%dn”, sum ) ; 13 int i, n ; i= 0 ; for( sum = 0 ; i < 10 ; sum+=n) { scanf( “%d”, &n ) ; i++ ; } printf( “%dn”, sum ) ; int i, n ; i= 0 ; sum = 0 ; while ( i< 10) { scanf( “%d”, &n ) ; i++ ; sum += n ; } printf( “%dn”, sum ) ; int i, n ; sum = 0 ; for( i = 0 ; i < 10 ; i++ ) { scanf( “%d”, &n ) ; sum += n ; } printf( “%dn”, sum ) ;
  • 14. 14 for 문  Counting up from 0 to n-1  Counting up from 1 to n  Counting up from n-1 to 0  Counting up from n to 1 for ( i = 0; i < n; i++ ) A; for ( i = 1; i <= n; i++ ) A; for ( i = n -1; i >= 0; i-- ) A; for ( i = n; i > 0; i-- ) A;
  • 15. break 문  break – break문을 감싸는 루프에서 빠져 나온다. 15 while(A) { B; while(C) { D; if( E ) break; F; } G; if( H ) break ; I ; } J;
  • 16. continue 문  continue – continue를 감싸는 루프의 시작점(조건확인)으로 올라간다. 16 while(A) { B; while(C) { D; if( E ) continue; F; } G; if( H ) continue ; I; } J;
  • 17. continue 문  continue 17 while(A) { B; while(C) { D; if( E ) continue; F; G; } H; if( I ) continue ; J; K; } L; while(A) { B; while(C) { D; if( !E ) { F; G; } } H; if( !I ) { J; K; } } L;
  • 18.  합이 7이 되는 양의 정수 3개의 리스트를 출력하시오. Example #include <stdio.h> int main(void) { int i, j, k ; for ( i = 0; i <= 7; ++i ) { for( j = 0; j <= 7; ++j ) { for ( k = 0; k <= 7; ++k ) { if ( i + j + k == 7 ) printf( "%d %d %dn", i, j, k ); } } } return 0; } 0 0 7 0 1 6 0 2 5 0 3 4 … 5 0 2 5 1 1 … 6 1 0 7 0 0 18
  • 19. Example 1  Add all integers between 1 and 100 19 Start Var i, sum Stop i  100 sum F T i <- 1 sum <- 0 i <- i + 1 sum <- sum + i #include <stdio.h> int main() { int i, sum ; i = 0 ; sum = 0 ; while( i <= 100 ) { sum = sum + i ; i = i + 1 ; } printf( “%d”, sum ) ; return 0; }
  • 20. Example 1-1  Add all integers between 1 and 100 20 Start Var i, sum Stop sum i <- 1 sum <- 0 i <- i + 1 sum <- sum + i #include <stdio.h> int main() { int i, sum ; i = 0 ; sum = 0 ; while( i <= 100 ) { sum = sum + i ; i = i + 1 ; } printf( “%d”, sum ) ; return 0; } i  100
  • 21. Example 2  Add all even integers between 1 and 100 21 Start Var i, sum Stop sum i <- 1, sum <- 0 i <- i + 1 sum <- sum + i i is even ? T F #include <stdio.h> int main() { int i, sum ; i = 0 ; sum = 0 ; while( i <= 100 ) { if( i % 2 == 0 ) { sum = sum + i ; } i = i + 1 ; } printf( “%d”, sum ) ; return 0; } i  100
  • 22. Example 3  Find the largest n such that 1+2+…+n<1000 22 Start Var n, sum Stop sum < 1000 n-1F T n <- 1, sum <- 0 n <- n + 1 sum <- sum + n #include <stdio.h> #include <math.h> int main() { int n, sum ; n = 1 ; sum = 0 ; while( 1) { sum = sum + n ; if( sum < 1000) break ; n = n + 1 ; } printf( “%d”, n-2 ) ; return 0; }
  • 23. Example 3-1  Find the largest n such that 1+2+…+n<1000 23 #include <stdio.h> #include <math.h> int main() { int n, sum ; n = 1 ; sum = 0 ; sum = sum + n ; while( sum < 1000 ) { n = n + 1 ; sum = sum + n ; } printf( “%d”, n-2 ) ; return 0; } Start Var n, sum Stop n-1 n <- 1, sum <- 0 n <- n + 1 sum <- sum + n sum <- sum + n sum < 1000
  • 24. Example 4  (1+1+…+1)+ … + (10+10+…+10) 24 Start Var a, b, sum a = 1, sum = 0 sum b = b + 1 sum = sum+ a Stop #include <stdio.h> #include <math.h> int main() { int a, b; a = 1 ; while( a <= 10 ) { b = 1 ; while( b <= 10 ) { sum = sum + a ; b = b + 1 ; } a = a + 1 ; } return 0; } a  10 b  10 b <- 1 a = a + 1
  • 25. Example 5  (2*1+2*2+…+2*9)+ … + (9*1+9*2+…+9*9) 25 Start Var a, b, sum a = 2, sum = 0 sum b = b + 1 sum = sum+ a * b Stop #include <stdio.h> #include <math.h> int main() { int a, b; a = 2 ; while( a <= 9 ) { b = 1 ; while( b <= 9 ) { sum = sum + a*b ; b = b + 1 ; } a = a + 1 ; } return 0; } a  9 b  9 b <- 1 a = a + 1
  • 26. Example 6  1+(1+2)+(1+2+3)+......+(1+....+10) 26 Start Var n, a, sum Stop sum n <- 1, sum <- 0 n <- n + 1 sum <- sum + a #include <stdio.h> #include <math.h> int main() { int n, a, sum ; n = 1 ; a = 1 ; sum = 0 ; while( n <= 10 ) { a = 1 ; while( a <= n ) { sum = sum + a ; a = a + 1 ; } n = n + 1 ; } printf( “%d”, sum ) ; return 0; } n  10 a <- 1 a  n a <- a + 1