네트워크 보안 수업 7일차 - 코리아 정보보안 IT학원

2016. 4. 5. 16:45네트워크 보안 수업/C언어

반응형

if-else


if(조건){

//if block

} else {

//else block

}


[실습]: sample09.c

#include <stdio.h>


int main(int argc, char *argv[])

{


if(10 > 100) {

printf("10 is not less then 100 \n");

} else {

printf("10 is less then 100 \n");

}


return 0;

}


다중조건


if(조건 && 조건 || 조건 ...) {


}


else if : 여러개의 조건


if( 조건 ) {


} else if(조건) {


} else if(조건) {


} ...

else {


}


[실습]: sample10.c

#include <stdio.h>


int main(int argc, char *argv[])

{


int score = 86;


if( score > 90) {

printf("A \n");

} else if(score > 80) {

printf("B \n");

} else if(score > 70) {

printf("C \n");

} else {

printf("F \n");

}


return 0;

}


[실습]

-임의의 두 수를 비교하여 큰 수를 출력하는 프로그램을 작성


#include <stdio.h>


int main(int argc, char *argv[])


{


int big = 1184;

int low = 1188;


if(big > low) {

printf("%d \n", big);

} else {

printf("%d \n", low);

}


return 0;

}


[실습]

- 임의의 세개의 수를 비교하여 큰 수를 출력하는 프로그램을 작성

#include <stdio.h>


int main(int argc, char *argv[])


{


int big = 500;

int middle = 300;

int low = 100;


if(big > middle && big > low){

printf("%d is bigger then %d and %d \n", big , middle, low);

} else if(middle > big && middle > low) {

printf("%d is bigger then %d and %d \n", middle, big, low);

} else if(low > big && low > middle) {

printf("%d is bigger then %d and %d \n", low, big, middle);

} else {

printf("%d and %d and %d is same);

}


return 0;

}


#include <stdio.h>


int big = 500;

int mid = 300;

int low = 100;


        if (big > middle && big > low){

                printf(" big is bigger then middle and low\n");

        }  else if(middle > big && middle > low) {

                printf("middle is bigger then big and low \n");

        } else if(low > big && low > middle) {

                printf("low is bigger then middle and big \n");

        } else if(big = middle && big > low) {

                printf("big and middle is same and bigger then low \n");

        } else if(big = low && big > middle) {

                printf("big and low is same and bigger then middle \n");

        } else if(low = middle && low > big) {

                printf("low and middle is same and bigger then big \n");

        } else {

                printf("big and middle and low is same \n", big, middle, low);

        }


        return 0;

}


switch


- 기본형태

switch(값){


case 값:


[실습]: sample11.c

#include <stdio.h>


int main(int argc, char *argv[])

{

int choice = 1;


switch(choice) {

case 1:

printf("select number 1 \n");


case 2:

printf("select number 2 \n");


case 3:

printf("select number 3 \n");


default:

printf("unknown number \n");

}


return 0;

}


[실습]: sample12.c

#include <stdio.h>


int main(int argc, char *argv[])


{


char choice = 0;


printf("select favorite fruits \n");

printf("a. banana \n");

printf("b. apple \n");

printf("c. orange \n");

printf("d. podo \n");

printf("----------------------------------------------\n");

printf("input char : \n");

choice = 'a';


switch(choice){

case 'a':

printf("selected banana \n");

break;

case 'b':

printf("selected apple \n");

break;

case 'c':

printf("selected orange \n");

break;

case 'd':

printf("selected podo \n");

break;

default:

printf("unknown \n");

}



return 0;

}



#include <stdio.h>


int main(int argc, char *argv[])


{


char choice = 0;


printf("select favorite fruits \n");

printf("a. banana \n");

printf("b. apple \n");

printf("c. orange \n");

printf("d. podo \n");

printf("----------------------------------------------\n");

printf("input char : \n");

choice = 'a';



if(choice = a){

printf("selected banana \n");

} else if(choice = b) {

printf("selected apple \n");

} else if(choice = c) {

printf("selected orange \n");

} else if(choice = d) {

printf("selected podo \n");

} else {

printf("unknown \n");

}


return 0;

}

반응형