카테고리 없음

4-4 다중 if문

Recircle 2019. 5. 16. 01:25
4-4 다중 if문

if 문에 다른 if문이 들어갈 수 있다.

1
2
3
4
5
6
7
if (조건식 1)
{
    if (조건식 2)
    {
  문장;
    }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
if ( score >= 80)
{
    if (score >= 90)
    {
        printf("A학점 입니다.")
    }
}
else
{
    printf("B학점 입니다.")
}
 
 
cs

위와 같이 구현이 가능하다. 이때 들여쓰기에 주의하자.



//성적을 받아서 학점을 결정하는 프로그램

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>
 
int main()
{
    int score;
 
    printf("점수를 입력해주세요    :");
    scanf_s("%d"&score);
 
    if (score >= 90)
        printf("A학점 \n");
    else if (score >= 80)
        printf("B학점 \n");
    else if (score >= 70)
        printf("C학점 \n");
    else if (score >= 60)
        printf("D학점 \n");
    else
        printf("F학점 \n");
 
    
    return 0;
 
}
cs