[C] 2강. 조건문

C 언어 - 조건문


조건문의 개념

Program의 구성 요소

수식 (Equation) : 연산자(+, -, *, /, %)와 피 연산자(숫자, 변수)로 이루어진 수식


Statement(명령문)


int result;
result = val / ratio; // 할당문
printf("%d", result); // 함수호출
if (a<0) result = 0; // 조건 문
while (result < 10) // 반복 문
    result += 1;


Block

{
    int result;
    result = sum;
    printf("%d", result);
}


Program

#include <stdio.h>

int num;

// Program의 실행은 항상 main 함수부터 시작됨
// 즉 main 함수는 program의 entry point
void main() {
    print("Hello! (%d)", num);
}


조건에 대한 반응


단순 조건 문

if 문

int a,b;
a = 10;
b = 9;

if (a>b) {
    printf("a는 b보다 크다.")
}


if {} else {} 문

int a,b;
a = 10;
b = 9;

if (a>b) {
    printf("a는 b보다 크다.")
} else {y
    printf("b는 a보다 크다.")
}


수식과 연산자

수학 프로그램 차이점
+ [더해라] + [더해라]  
- [빼라] - [빼라]  
x [곱해라] * [곱해라]  
% [나누어라] / [나누어라]  
  % [나머지 구해라] 추가
> [크다] >[큰가?] 다른의미
>= [크거나같다] >= [크거나 같나?] 다른의미
< [작다] < [작은가?] 다른의미
<= [작거나 같다] <= [작거나 같나?] 다른의미
= [같다] == [같은가?] 다른의미
!= [다르다] != [다른가?] 다른의미
  = [좌측에 넣어라] 추가


int a,b;
scanf("%d", &a);
scanf("%d", &b);
printf("%d, %d₩n" a,b);
if (a > b)
    printf("%d", a);
else
    printf("%d", b);


중첩된 조건 문

#include<stdio.h>

main() {
    int age;
    scanf("%d", &age);
    if (age < 8)
        printf("1알");
    else if (age < 15)
        printf("1.5알");
    else
        printf("2알");
}