if && else if && else 의 활용
2022. 2. 23. 23:09
1
2
3
4
|
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
|
cs |
기본적으로, 라이브러리란 자주 사용하는 함수들을 미리 작성하여 저장해둔 파일로써 보통 헤더파일로 저장됨.
C언어에서 기본적으로 사용하는 헤더파일은 stdio.h 이며 이 stdio.h 는 Standard Input Ouput(표준 입출력)의 약자.
이 헤더파일은 #include 라는 코드를 통해 추가해 줄 수 있음.
rand()함수는 stdlib.h 헤더파일에 포함되어있기 때문에 코드 맨 윗줄에 #include <stdlib.h>를 작성.
time.h는 타임 값을 받아오는 헤더파일.
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
|
int main(void){
// if / else if / else
int age = 15;
if (age >= 20){
printf("ordinary guy");
}
else{
printf("student");
}
int age = 18;
if (age >= 8 && age <= 13){
printf("elementary school");
}
else if (age >= 14 && age <= 16)
{
printf("middle schoole");
}
else if (age >= 17 && age <= 19){
printf("high school");
}
else{
printf("adult");
}
return 0;
}
|
cs |
위와 같이 if / else 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
|
// break / continue
for (int i = 1; i<= 30; i++){
if(i>= 6){
printf("the rest can go home");
break;
}
printf("%d th student have to ready for presentation\n", i);
}
continue
for (int i = 1; i<=30; i++)
{
if(i>= 6 && i <=10){
if(i == 7)
{
printf("%d th is empty\n", i);
continue; // return to 'for' sentence
}
printf("%d th have to ready for presentation\n", i);
}
}
|
cs |
if-else 문에서 위와 같이 break를 주어 문장을 끝낼 수 있고, contine를 주어 처음으로 돌아갈 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
// && ||
int a = 10;
int b = 10;
int c = 12;
int d = 13;
if (a == b || c == d){
printf("a and b or c and d are same");
}
else{
printf("difference");
}
|
cs |
and는 &&로 or은 ||로 식에 넣을 수 있다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// rock sisors paper
srand(time(NULL));
int i = rand() % 3; // 0 ~ 2
if (i == 0){
printf("rock\n");
}
else if (i == 1){
printf("sisors");
}
else if (i == 2){
printf("paper");
}
else {
printf("i don't know");
}
|
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
33
|
// switch case
srand(time(NULL));
int i = rand() % 3; // 0 ~ 2
switch( i )
{
case 0:printf("rock\n");break;
case 1:printf("sisors");break;
case 2:printf("paper");break;
default:printf("i don't know");break;
}
int age = 11;
switch (age)
{
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:printf("elementary school"); break;
case 14:
case 15:
case 16:printf("middle school"); break;
case 17:
case 18:
case 19:printf("high school"); break;
default:
printf("adlut"); break;
}
|
cs |
위와 같이 switch문에서도 여러 가지를 구현할 수 있다.
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
33
|
// UP AND DOWN
srand(time(NULL));
int num = rand() % 100 + 1; // 0 ~ 100
printf("number : %d", num);
int answer = 0;
int chance = 5;
while (chance > 0)
{
printf("the rest chance : %d\n", chance--);
printf("select the number (1~100) : ");
scanf("%d", &answer);
if (answer > num){
printf("DOWN \n\n");
}
else if (answer < num){
printf("UP \n\n");
}
else if (answer == num){
printf("great !");
break;
}
else {
printf("bye");
}
if(chance == 0){
printf("no chance");
break;
}
}
|
cs |
UP and DOWN game 구현
1
2
3
4
5
|
return 0;
}
|
cs |
위와 같이 코드 마무리 할 수 있다.
출처 :https://www.youtube.com/watch?v=q6fPjQAzll8&t=494s
'Programming > C\C++' 카테고리의 다른 글
struct 구조체 (0) | 2022.03.04 |
---|---|
MultiDimensional Array (0) | 2022.03.04 |
*포인터* (0) | 2022.03.04 |
Initial Array && ASCII (0) | 2022.02.25 |
functions setting 과 활용 (0) | 2022.02.25 |