SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
IF Statement
3. 비교문변환 (1)
 Type 1  Type 2
2
조건 T
F
D
C
B
A
E
if( 조건 )
{
A ;
B ;
}
else
{
C ;
D ;
}
E ;
조건 T
F
D
C
B
A
if( 조건 )
{
A ;
B ;
}
C ;
D ;
2. 비교문 변환 (2)
 if 문
3
if( 조건 )
{
A ;
B ;
}
else
{
C ;
D ;
}
E ;
조건이 만족되면 하는 일
조건이 거짓이면 하는 일
if 문 다음에 할 문장
조건 T
F
D
C
B
A
E
2. 비교문 변환 (3)
 Example
4
조건1 T
F
B C
D
A
조건2
E
if( 조건1 )
{
A;
}
else
{
if( 조건2 )
{
C ;
}
else
{
B ;
}
D ;
}
E ;
TF
Example 2
 Read a number and print “Yes” if it is 2 or “No”
5
Start
x
x
“Yes”
Stop
x = 2
“No”
T F
#include <stdio.h>
int main() {
int x ;
scanf( “%d”, &x ) ;
if( x == 2 )
{
printf( “Yesn” ) ;
}
else
{
printf( “Non” ) ;
}
return 0;
}
Example 3
 Find the maximum among 3 numbers
6
Start
Var a, b, c
Stop
a > b
a
T F
a > c
Input a, b, c
b > c
c b c
#include <stdio.h>
int main() {
int a, b, c ;
scanf( %d%d%d”, &a, &b, &c ) ;
if(a > b )
{
if(a > c ) {
printf( “%dn”, a ) ;
} else {
printf( “%dn”, c ) ;
}
}
else
{
if( b > c ) {
printf( “%dn”, b ) ;
} else {
printf( “%dn”, c ) ;
}
}
return 0;
}
Example 4
 Sort 3 numbers
7
Start
a, b, c
Stop
a > b
a, b, c
T F
b > c
a, b, c
a > c
a, c, b b, a, c b, c, a
a > c b > c
c, a, b c, b, a
T F T F
T F T F
#include <stdio.h>
int main() {
int a, b, c ;
scanf( %d%d%d”, &a, &b, &c ) ;
if( a > b )
{
if( b > c ) {
printf( “%d %d %dn”, a, b, c ) ;
} else {
if( a > c ) {
printf( “%d %d %dn”, a, c, b ) ;
} else {
printf( “%d %d %dn”, c, a, b ) ;
}
}
}
else
{
if( a > c ) {
printf( “%d %d %dn”, b, a, c ) ;
} else {
if( b > c ) {
printf( “%d %d %dn”, b, c, a ) ;
} else {
printf( “%d %d %dn”, c, b, a ) ;
}
}
}
return 0;
}
8
if and if-else Statements
 Nested if statement if ( 조건1 ) {
A;
}
else {
if( 조건2 ) {
B;
}
else {
if( 조건3 ) {
C;
}
else {
D;
}
}
}
E;
조건1
조건2
조건3
D
E
A
B
C
T
F
T
F
T
F
9
if and if-else Statements
 Nested if statement
if ( 조건1 ) {
A;
}
else if( 조건2 ) {
B;
}
else if( 조건3 ) {
C;
}
else {
D;
}
E;
조건1
조건2
조건3
D
E
A
B
C
T
F
T
F
T
F
10
if and if-else Statements
 Nested if statement
– 문자를 입력 받아 대문자, 소문자, 숫자, 그 외 문자인지 확인
하시오.
scanf( “%c”, &ch ) ;
if (‘0’ <= c && c <= ‘9’)
printf( “숫자n” ) ;
else if ( ‘A’ <= c && c <= ‘Z’)
printf( “대문자n” ) ;
else if (‘a’ <= c && c <= ‘z’)
printf( “소문자n” ) ;
else
printf( “그외 문자n” ) ;
switch 문
 특정형태의 Nested if statement
11
변수==값1
변수==값2
변수==값3
D
E
A
B
C
T
F
T
F
T
F
switch( 변수 ) {
case 값1 : A ;
break ;
case 값2 : B ;
break ;
case 값3 : C ;
break ;
default :
D ;
}
switch 문
 swtich문을 Nested if 문으로 변환하기
12
switch( 변수 ) {
case 값1 : A ;
break ;
case 값2 : B ;
break ;
case 값3 : C ;
break ;
default : D ;
}
if( 변수==값1 ) {
A ;
}
else if(변수==값2 ) {
B ;
}
else if(변수==값3 ) {
C ;
}
else {
D ;
}
switch 문
 switch에서 break의 역할
– grade == 4 일 때
13
switch (grade)
{
case 4 : printf(“A”) ;
break;
case 3 : printf(“B”) ;
break;
case 2 : printf(“C”) ;
break;
case 1 : printf(“D”) ;
break;
default : printf(“Illegal grade”);
}
switch (grade)
{
case 4 : printf(“A”) ;
case 3 : printf(“B”) ;
case 2 : printf(“C”) ;
case 1 : printf(“D”) ;
default : printf(“Illegal grade”);
}
switch 문
 switch에서 break의 역할
– grade가 ‘A’, ‘B’, ‘C’, ‘D’ 이면 “pass”, ‘F’이면 “fail”, 그 외는
“error”를 출력
14
switch (grade) {
case ‘A’ :
case ‘B’ :
case ‘C’ :
case ‘D’ : printf(“passn”); break;
case ‘F’ : pritnf(“failn”); break;
default : printf(“errorn”); break;
}
15
switch 문
 switch에서 break의 역할
– 앞의 예제를 if-else로 변환
if ( grade == ‘A’ || grade == ‘B’ || grade == ‘C’ || grade == ‘D’ )
printf( “passn” ) ;
else if( grade == ‘F’ )
printf( “failn” ) ;
else
printf( “errorn” ) ;
16
Short-Circuit
 Short-Circuit Evaluation
– &&와 ||의 연산에서 true와 false의 값이 결정됨과 동시에 계
산과정은 멈추게 된다.
 expr1 && expr2
– expr1의 결과가 F이면, expr2 비교가 수행되지 않음.
 expr1 || expr2
– expr1의 결과가 T이면, expr2 비교가 수행되지 않음.
17
Short-Circuit
 Short-Circuit Evaluation의 예
int i=2, j=3;
if( (i == 2) && ( j++ == 3) )
printf( “Truen” ) ;
else
printf( “Falsen” ) ;
printf(“%d %dn”, i, j);
int i=2, j=3;
if( (i == 2) || (j++ == 3) )
printf( “Truen” ) ;
else
printf( “Falsen” ) ;
printf(“%d %dn”, i, j);
int i=2, j=3;
if( (i == 3) && ( j++ == 3) )
printf( “Truen” ) ;
else
printf( “Falsen” ) ;
printf(“%d %dn”, i, j);
int i=2, j=3;
if( (i == 3) || (j++ == 3) )
printf( “Truen” ) ;
else
printf( “Falsen” ) ;
printf(“%d %dn”, i, j);
18
Conditional Operator
 Conditional Operator Syntax
– 조건식 연산자 ? : 는 삼항 연산자이다.
– expr1이 먼저 계산된 후, 참이면 expr2가 실행, 거짓이면
expr3이 수행된다.
 if-else 문 Conditional Operator
expr1 ? expr2 : expr3
if ( y < z)
x = y;
else
x = z
x = ( y < z ) ? y : z
19
Conditional Operator
 Example
int a = 5, b = 6 ;
int max = (a < b) ? b : a ;
int min = (a > b) ? b : a ;

Weitere ähnliche Inhalte

Andere mochten auch

5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
12 1. const pointer, typedef
12 1. const pointer, typedef12 1. const pointer, typedef
12 1. const pointer, typedef웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 

Andere mochten auch (9)

5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
6. functions
6. functions6. functions
6. functions
 
Goorm class
Goorm classGoorm class
Goorm class
 
6 function
6 function6 function
6 function
 
skku cp2 w4
skku cp2 w4skku cp2 w4
skku cp2 w4
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
12 1. const pointer, typedef
12 1. const pointer, typedef12 1. const pointer, typedef
12 1. const pointer, typedef
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 

Ähnlich wie 3 2. if statement

C수업자료
C수업자료C수업자료
C수업자료koominsu
 
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)코딩인카페 C&JAVA 기초과정 C프로그래밍(2)
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)유익아카데미
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차Yeonah Ki
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차Han Sung Kim
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성Lee Sang-Ho
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차Yeonah Ki
 
[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
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfjinwookhong
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdfkd19h
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3guestc0587d1
 
2012 Dm C2 03
2012 Dm C2 032012 Dm C2 03
2012 Dm C2 03seonhyung
 

Ähnlich wie 3 2. if statement (16)

C수업자료
C수업자료C수업자료
C수업자료
 
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)코딩인카페 C&JAVA 기초과정 C프로그래밍(2)
코딩인카페 C&JAVA 기초과정 C프로그래밍(2)
 
선택문
선택문선택문
선택문
 
선택문
선택문선택문
선택문
 
C review
C  reviewC  review
C review
 
게임프로그래밍입문 4주차
게임프로그래밍입문 4주차게임프로그래밍입문 4주차
게임프로그래밍입문 4주차
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차
 
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
[방송통신대 컴퓨터과학과] C 프로그래밍 과제물 작성
 
게임프로그래밍입문 3주차
게임프로그래밍입문 3주차게임프로그래밍입문 3주차
게임프로그래밍입문 3주차
 
[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
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
이산치2번
이산치2번이산치2번
이산치2번
 
2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf2012 Dm A0 02 Pdf
2012 Dm A0 02 Pdf
 
3.포인터
3.포인터3.포인터
3.포인터
 
(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3(학생용)+프로그래밍+및+실습 Chap4 3
(학생용)+프로그래밍+및+실습 Chap4 3
 
2012 Dm C2 03
2012 Dm C2 032012 Dm C2 03
2012 Dm C2 03
 

Mehr von 웅식 전

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 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 

Mehr von 웅식 전 (20)

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
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
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 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 

3 2. if statement

  • 2. 3. 비교문변환 (1)  Type 1  Type 2 2 조건 T F D C B A E if( 조건 ) { A ; B ; } else { C ; D ; } E ; 조건 T F D C B A if( 조건 ) { A ; B ; } C ; D ;
  • 3. 2. 비교문 변환 (2)  if 문 3 if( 조건 ) { A ; B ; } else { C ; D ; } E ; 조건이 만족되면 하는 일 조건이 거짓이면 하는 일 if 문 다음에 할 문장 조건 T F D C B A E
  • 4. 2. 비교문 변환 (3)  Example 4 조건1 T F B C D A 조건2 E if( 조건1 ) { A; } else { if( 조건2 ) { C ; } else { B ; } D ; } E ; TF
  • 5. Example 2  Read a number and print “Yes” if it is 2 or “No” 5 Start x x “Yes” Stop x = 2 “No” T F #include <stdio.h> int main() { int x ; scanf( “%d”, &x ) ; if( x == 2 ) { printf( “Yesn” ) ; } else { printf( “Non” ) ; } return 0; }
  • 6. Example 3  Find the maximum among 3 numbers 6 Start Var a, b, c Stop a > b a T F a > c Input a, b, c b > c c b c #include <stdio.h> int main() { int a, b, c ; scanf( %d%d%d”, &a, &b, &c ) ; if(a > b ) { if(a > c ) { printf( “%dn”, a ) ; } else { printf( “%dn”, c ) ; } } else { if( b > c ) { printf( “%dn”, b ) ; } else { printf( “%dn”, c ) ; } } return 0; }
  • 7. Example 4  Sort 3 numbers 7 Start a, b, c Stop a > b a, b, c T F b > c a, b, c a > c a, c, b b, a, c b, c, a a > c b > c c, a, b c, b, a T F T F T F T F #include <stdio.h> int main() { int a, b, c ; scanf( %d%d%d”, &a, &b, &c ) ; if( a > b ) { if( b > c ) { printf( “%d %d %dn”, a, b, c ) ; } else { if( a > c ) { printf( “%d %d %dn”, a, c, b ) ; } else { printf( “%d %d %dn”, c, a, b ) ; } } } else { if( a > c ) { printf( “%d %d %dn”, b, a, c ) ; } else { if( b > c ) { printf( “%d %d %dn”, b, c, a ) ; } else { printf( “%d %d %dn”, c, b, a ) ; } } } return 0; }
  • 8. 8 if and if-else Statements  Nested if statement if ( 조건1 ) { A; } else { if( 조건2 ) { B; } else { if( 조건3 ) { C; } else { D; } } } E; 조건1 조건2 조건3 D E A B C T F T F T F
  • 9. 9 if and if-else Statements  Nested if statement if ( 조건1 ) { A; } else if( 조건2 ) { B; } else if( 조건3 ) { C; } else { D; } E; 조건1 조건2 조건3 D E A B C T F T F T F
  • 10. 10 if and if-else Statements  Nested if statement – 문자를 입력 받아 대문자, 소문자, 숫자, 그 외 문자인지 확인 하시오. scanf( “%c”, &ch ) ; if (‘0’ <= c && c <= ‘9’) printf( “숫자n” ) ; else if ( ‘A’ <= c && c <= ‘Z’) printf( “대문자n” ) ; else if (‘a’ <= c && c <= ‘z’) printf( “소문자n” ) ; else printf( “그외 문자n” ) ;
  • 11. switch 문  특정형태의 Nested if statement 11 변수==값1 변수==값2 변수==값3 D E A B C T F T F T F switch( 변수 ) { case 값1 : A ; break ; case 값2 : B ; break ; case 값3 : C ; break ; default : D ; }
  • 12. switch 문  swtich문을 Nested if 문으로 변환하기 12 switch( 변수 ) { case 값1 : A ; break ; case 값2 : B ; break ; case 값3 : C ; break ; default : D ; } if( 변수==값1 ) { A ; } else if(변수==값2 ) { B ; } else if(변수==값3 ) { C ; } else { D ; }
  • 13. switch 문  switch에서 break의 역할 – grade == 4 일 때 13 switch (grade) { case 4 : printf(“A”) ; break; case 3 : printf(“B”) ; break; case 2 : printf(“C”) ; break; case 1 : printf(“D”) ; break; default : printf(“Illegal grade”); } switch (grade) { case 4 : printf(“A”) ; case 3 : printf(“B”) ; case 2 : printf(“C”) ; case 1 : printf(“D”) ; default : printf(“Illegal grade”); }
  • 14. switch 문  switch에서 break의 역할 – grade가 ‘A’, ‘B’, ‘C’, ‘D’ 이면 “pass”, ‘F’이면 “fail”, 그 외는 “error”를 출력 14 switch (grade) { case ‘A’ : case ‘B’ : case ‘C’ : case ‘D’ : printf(“passn”); break; case ‘F’ : pritnf(“failn”); break; default : printf(“errorn”); break; }
  • 15. 15 switch 문  switch에서 break의 역할 – 앞의 예제를 if-else로 변환 if ( grade == ‘A’ || grade == ‘B’ || grade == ‘C’ || grade == ‘D’ ) printf( “passn” ) ; else if( grade == ‘F’ ) printf( “failn” ) ; else printf( “errorn” ) ;
  • 16. 16 Short-Circuit  Short-Circuit Evaluation – &&와 ||의 연산에서 true와 false의 값이 결정됨과 동시에 계 산과정은 멈추게 된다.  expr1 && expr2 – expr1의 결과가 F이면, expr2 비교가 수행되지 않음.  expr1 || expr2 – expr1의 결과가 T이면, expr2 비교가 수행되지 않음.
  • 17. 17 Short-Circuit  Short-Circuit Evaluation의 예 int i=2, j=3; if( (i == 2) && ( j++ == 3) ) printf( “Truen” ) ; else printf( “Falsen” ) ; printf(“%d %dn”, i, j); int i=2, j=3; if( (i == 2) || (j++ == 3) ) printf( “Truen” ) ; else printf( “Falsen” ) ; printf(“%d %dn”, i, j); int i=2, j=3; if( (i == 3) && ( j++ == 3) ) printf( “Truen” ) ; else printf( “Falsen” ) ; printf(“%d %dn”, i, j); int i=2, j=3; if( (i == 3) || (j++ == 3) ) printf( “Truen” ) ; else printf( “Falsen” ) ; printf(“%d %dn”, i, j);
  • 18. 18 Conditional Operator  Conditional Operator Syntax – 조건식 연산자 ? : 는 삼항 연산자이다. – expr1이 먼저 계산된 후, 참이면 expr2가 실행, 거짓이면 expr3이 수행된다.  if-else 문 Conditional Operator expr1 ? expr2 : expr3 if ( y < z) x = y; else x = z x = ( y < z ) ? y : z
  • 19. 19 Conditional Operator  Example int a = 5, b = 6 ; int max = (a < b) ? b : a ; int min = (a > b) ? b : a ;