Booleans and selection: if, else, switch
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
English
True and false in C
- C has no separate true/false type by default. It uses numbers:
0means false, and any other number means true. - A test like
5 > 3produces1(true).5 < 3produces0(false). - (If you
#include <stdbool.h>you can writebool,true, andfalse— but plainintworks everywhere.)
한국어
C의 참과 거짓
- C는 기본적으로 true/false 타입이 없습니다. 숫자를 사용합니다:
0은 거짓(false), 그 외의 모든 숫자는 참(true) 을 의미합니다. 5 > 3같은 테스트는1(참)을 생성합니다.5 < 3은0(거짓)을 생성합니다.- (
#include <stdbool.h>을 if하면bool,true, 그리고false을 쓸 수 있지만, plainint은 어디든 작동합니다.)
English
Comparing and combining
- Compare values with
==(equal),!=(not equal),<,>,<=,>=. - Be careful:
==tests equality; a single=assigns a value. They are different! - Combine tests with
&&(and),||(or), and!(not).a && bis true only when both are true.
한국어
비교 및 결합
==(같음),!=(다름),<,>,<=,>=으로 값을 비교하세요.- 주의하세요:
==은 등식을 테스트하지만, 단일=은 값을 대입(assign) 합니다. 서로 다릅니다! &&(and),||(or),!(not)으로 테스트를 결합하세요.a && b은 두 조건 모두 참일 때만 참입니다.
English
if / else if / else
if (test) { ... }runs the block only when the test is true.- Add
else if (...)to check another test, andelsefor "none of the above". - Only the first matching branch runs.
한국어
if / else if / else
if (test) { ... }은 테스트가 참일 때만 블록을 실행합니다.- 다른 테스트를 확인하려면
else if (...)을 추가하고, '위 none of the above'를 위해else을 사용하세요. - 일치하는 첫 번째 분기만 실행됩니다.
#include <stdio.h>
int main(void) {
int score = 72;
if (score >= 90) {
printf("A\n");
} else if (score >= 60) {
printf("Pass\n"); // this one runs
} else {
printf("Fail\n");
}
return 0;
}
English
switch: choosing among many values
- A
switchpicks a branch based on one value (anintorchar). - Each
caseends withbreak;so it does not fall into the next one.default:catches everything else. switchis neat when you compare the same variable against many fixed values.
한국어
switch: 여러 값 중 선택하기
switch은 하나의 값(int또는char)에 따라 분기를 선택합니다.- 각
case은break;으로 끝나므로 다음 분기로 넘어가지 않습니다.default:은 나머지 모든 것을 잡습니다. - 동일한 변수를 여러 고정값과 비교할 때
switch이 깔끔합니다.
English
Common mistakes
- Use
==to compare;=assigns (andif (x = 5)is always true). - Each
switchcase needs abreak;.
한국어
흔한 실수
- 비교에는
==을 사용하고, 대입에는=을 사용하세요 (if (x = 5)은 항상 참입니다). - 각
switch케이스에는 반드시break;이 필요합니다.
English
Now you try
- Use comparisons and
&&/||to build your tests. Remember0is false, anything else is true. - For the
switchtask, divide the score by 10 first, then switch on the result. - Do not write a
main— the checker provides one.
한국어
이제 직접 해보기
- 비교와
&&/||을 사용하여 테스트를 만드세요.0은 거짓이고 그 anything else is true. switch과제에서는 점수를 10으로 나눈 뒤 결과에 대해 switch를 실행하세요.main을 작성하지 마세요; 검증程序가 제공해 줍니다.
Explore · 탐색하기
if / else if / else
Conditions test top-down; the first true block runs.
Complete int sign(int n) so it returns -1 when n is negative, 0 when n is zero, and 1 when n is positive. Do not write a main.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Complete int in_range(int n, int lo, int hi) so it returns 1 when n is between lo and hi (inclusive), and 0 otherwise. Use &&. Do not write a main.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Complete char grade_letter(int score) using a switch on score / 10. Return 'A' for 90–100, 'B' for the 80s, 'C' for the 70s, 'D' for the 60s, and 'F' below 60. Do not write a main.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.