forked from aed-i-2024-q1/student-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
control_structures.c
139 lines (112 loc) · 2.17 KB
/
control_structures.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* Control Structures
*
* - if-else
* - if-else-if-else
* - switch-case
* - while
* - do-while
* - for
* - break
* - continue
* - goto
* - ternary operator
*/
#include <stdio.h>
void if_else(void) {
int x = 10;
if (x == 10) {
printf("x is equal to 10\n");
}
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}
}
void if_elseif_else(void) {
int x = 15;
if (x == 10) {
printf("x is equal to 10\n");
} else if (x > 5) {
printf("x is greater than 5\n");
} else if (x == 5) {
printf("x is equal to 5\n");
} else {
printf("x is less than 5\n");
}
}
void switch_case(void) {
int x = 10;
switch (x) {
case 5:
printf("x is equal to 5\n");
break;
case 10:
printf("x is equal to 10\n");
break;
default:
printf("x is not equal to 5 or 10\n");
}
}
void while_loop(void) {
int x = 0;
while (x < 10) {
printf("%d\n", x);
x++;
}
}
void do_while_loop(void) {
int x = 0;
do {
printf("%d\n", x);
x++;
} while (x < 10);
}
void for_loop(void) {
for (int x = 0; x < 10; x++) {
printf("%d\n", x);
}
}
void break_statement(void) {
for (int x = 0; x < 10; x++) {
if (x == 5) {
break;
}
printf("%d\n", x);
}
}
void continue_statement(void) {
for (int x = 0; x < 10; x++) {
if (x == 5) {
continue;
}
printf("%d\n", x);
}
}
void goto_statement(void) {
int x = 0;
loop:
printf("%d\n", x);
x++;
if (x < 10) {
goto loop;
}
}
void ternary_operator(void) {
int x = 10;
x > 5 ? printf("x is greater than 5\n") : printf("x is less than or equal to 5\n");
}
int main(void) {
if_else();
// if_elseif_else();
// switch_case();
// while_loop();
// do_while_loop();
// for_loop();
// break_statement();
// continue_statement();
// goto_statement();
// ternary_operator();
return 0;
}