🖥C & C++/인프런 기초학습

[C/C++] 12강~14강 정리

728x90
반응형

<12강. 경우 나누기> 


#include <stdio.h>


int main(){

    int n;

    scanf("%d",&n);

    

    if (n % 2 ==0){    // 2로 나눈 나머지가 0이라면

        printf("n은 짝수\n");

    }

    else{

        printf("n은 홀수\n");

    }

}


if문에 대하여. python과 별 다를게 없음!


if () { }안에 else가 들어가지 않고 밖에서 else를 쓴다. indent만 맞추면 됨!



#include <stdio.h>


int main(){

    int n;

    scanf("%d",&n);

    

    if (n > 0){    // 2로 나눈 나머지가 0이라면

        printf("n은 양수\n");

    }

    else if (n == 0){

        printf("n은 0\n");

    }

    else {

        printf("n은 음수\n");

    }

}



R 처럼 else if ( ) { } 처럼 사용한다. Python의 elif 와 같은 것!


똑같이 else if 를 더 추가할 수 있다. 


<13강. if 문 잘 쓰기>


중첩 if문


중괄호 코딩 스타일


#include <stdio.h>


int main(){

    int a,b,c;

    

    scanf("%d%d%d",&a,&b,&c);  // 중복이 없다고 가정

    

    if (a > b){

        // a > b > c

        // a > c > b

        // c > a > b

        if (a > c){

            printf("a가 최댓값이다\n");

        }

        else {

            printf("c가 최댓값이다\n");

        }

    }

    else {

        if (b > c) {

            printf("b가 최댓값이다\n");

        }

        else {

            printf("c가 최댓값이다\n");

        }

    }

    

    

}




" ; " 세미콜론이 if문 안에 하나인 경우 { } 중괄호를 생략할 수 있다. 


#include <stdio.h>


int main(){

    int a,b,c;

    

    scanf("%d%d%d",&a,&b,&c);  // 중복이 없다고 가정

    

    if (a > b){

        // a > b > c

        // a > c > b

        // c > a > b

        if (a > c)

            printf("a가 최댓값이다\n");

        

        else

            printf("c가 최댓값이다\n");

        

    }

    else {

        if (b > c)

            printf("b가 최댓값이다\n");

        

        else

            printf("c가 최댓값이다\n");

        

    }

    

    

}


이것도 똑같이 실행됨!


#include <stdio.h>


int main(){

    int a,b,c;

    

    

    scanf("%d%d%d",&a,&b,&c);  // 중복이 없다고 가정

    

    if (a > b){

        if (a > c) printf("a가 최댓값이다\n");

        else printf("c가 최댓값이다\n");

    }

    else if (b > c) printf("b가 최댓값이다\n");

    else printf("c가 최댓값이다\n");

}


들여쓰기 없이 이렇게 한 줄로 작성해도 된다. 


위 처럼 줄여서 작성할 수 있다. 


하지만 가독성이 조금 떨어진다.


<14강. switch 와 goto 문>


if문 보다 조금 더 간단한 형태



#include <stdio.h>


int main() {

    int choice;

    

    printf("새 게임 : 1\n");

    printf("불러오기 : 2\n");

    printf("설정 : 3\n");

    printf("크레딧 : 4\n");

    

    scanf("%d",&choice);

    

    if (choice == 1){

        printf("새게임");

    }

    else if (choice == 2){

        printf("불러오기");

    }

    else if (choice == 3){

        printf("설정");

    }

    else if (choice == 4){

        printf("크레딧");

    }

    else {

        printf("키 입력 오류");

    }

}


if문 사용시 위처럼 할 수 있다. 


switch를 사용하면 아래처럼 된다.


#include <stdio.h>


int main() {

    int choice;

    

    printf("새 게임 : 1\n");

    printf("불러오기 : 2\n");

    printf("설정 : 3\n");

    printf("크레딧 : 4\n");

    

    scanf("%d",&choice);

    

    switch (choice){

    case 1:

        printf("새게임");

        break;

            

    case 2:

        printf("불러오기");

        break;

    case 3:

        printf("설정");

        break;

    case 4:

        printf("크레딧");

        break;

            

    default:

        printf("잘못 입력");

        break;

    }

}


break를 해서 중괄호 밖으로 나가줘야함/ break 하지 않으면 1임에도 불구하고 case 2로 넘어가게 된다.


좀 더 직관적이고 이해하기 쉽긴하다.



#include <stdio.h>


int main() {

    int choice;

    

    makeChoice:

    

    printf("새 게임 : 1\n");

    printf("불러오기 : 2\n");

    printf("설정 : 3\n");

    printf("크레딧 : 4\n");

    

    scanf("%d",&choice);

    

    switch (choice){

    case 1:

        printf("새게임");

        break;

            

    case 2:

        printf("불러오기");

        break;

    case 3:

        printf("설정");

        break;

    case 4:

        printf("크레딧");

        break;

            

    default:

        printf("잘못 입력\n");

        goto makeChoice;

        break;

    }

}


이는 go to 를 사용한 것인데 까먹어도 된다!


makeChoice를 해둔 곳으로 다시 돌아가게 된다. 기회를 더 주는 것!


go to가 3개만 되도 복잡해져서 알아볼 수 없다. 이런 걸 "스파게티 코드"라고 한다. 


그러니 사용하지 말자~ go to가 유용하지만 다른 방식으로 구현할 수 있다. 

728x90
반응형