ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C Language] 15. if ~ else - C 언어
    CSE/C Language 2015. 6. 13. 10:14

    1. 사용방법

     

     형  식

    예  제 

     if(조건식) 수행문;

    ▶ if (num == 5)

           puts("This is five");

    ▶ if (num == 5) puts("This is five"); 

     if(조건식) 

     {

          수행문;

          수행문;

          ...

     }

    ▶ if (character == 'c') 

       {

            puts("This is character 'c'.");

            puts("I don't like this");

       } 


    ▶ if (x > 90) 

       {

            puts("훌륭한 점수 입니다.");

            x = 0;

       }


    ▶ if (x >= 90 && x <= 100) 

       {

            puts("당신은 A 학점입니다");

       }

     if(조건식)

        수행문;

     else

        수행문;

    ▶ if (x > 60) 

            puts("당신의 점수는 별로군요.");

       else 

            puts("당신이 분발했다면 나았을 텐데..."); 

     if(조건식)

     {

        수행문;

        ...

     } 

     else 

     {

        수행문;

        ...

     }

    ▶ if (x % 2 == 0)

       {

           puts("x의 값은 짝수입니다.");

           x++;

       } 

       else 

       {

           puts("x의 값은 홀수입니다.");

           x++;

       }





    2. 다중 if ~ else

     다중 if ~ else는 여러 가지 상황 중에서 하나를 선택하거나 특정한 값이 어떠한 범위에 포함되어 있는지 판별하여 적절한 행위를 취하도록 할 때 유용하다.


     문장 1;

     if (조건식1)

    문장 2;

     else if (조건식2)

    문장3;

     else if (조건식3)

    문장4;

     else 

    문장5;

     문장6;

     



    위 다중 if ~ else 문을 도식화하여 보도록 하자.















    실전 예제를 통해 살펴보자. 키와 몸무게를 입력받아 표준체형인지 출력하는 예제이다.


     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    /*
     * bodytype.c
     *
     *  Created on: 2015. 5. 12.
     *      Author: root
     */
     
    #include <stdio.h>
     
    int main(void) {
     
        int height;
        int weight;
        int standard;
     
        puts("Input your tall and weight: ");
        scanf("%d %d", &height, &weight);
     
        standard = (int) ((height - 100* 0.9);
     
        if ((standard - weight) > 5)
            puts("PePero");
        else if ((standard - weight) < -5)
            puts("DDongDDong");
        else
            puts("Standard body type!!");
     
        return 0;
    }
    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
    25
    26
    27
    28
    29
    30
    31
    32
    /*
     * operation.c
     *
     *  Created on: 2015. 5. 12.
     *      Author: root
     */
     
    #include <stdio.h>
     
    int main(void) {
     
        int one, two;
        char operator;
     
        puts("Input num, operator, num(ex: 5 * 12):");
        scanf("%d %c %d", &one, &operator, &two);
     
        if (operator == '+')
            printf("%d + %d = %d", one, two, one + two);
        else if (operator == '-')
            printf("%d - %d = %d", one, two, one - two);
        else if (operator == '*')
                printf("%d * %d = %d", one, two, one * two);
        else if (operator == '/') {
            if (two == 0)
                puts("argument two is Zero!!");
            else
                printf("%d / %d = %d", one, two, one / two);
        }
     
        return 0;
    }
    cs





    * Programming in C 서적을 참고하여 작성하였습니다 


    댓글

Designed by Tistory.