-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathwhile.c
50 lines (38 loc) · 1.04 KB
/
while.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
/* # while */
#include "common.h"
int main(void) {
/* Basic example. */
{
int i = 0;
int is[] = { 0, 1, 2 };
while (i < 3) {
assert(i == is[i]);
i++;
}
assert(i == 3);
}
/* # do-while */
{
int i = 0;
int i2;
int is[] = { 0, 1, 2 };
do {
i2 = 2*i*i + 3*i + (i % 2);
assert(i == is[i]);
i++;
} while (i2 < 7);
/* Don't forget the ';'. */
/*
Application Loop must execute at least once to know if it will continue.
Without do-while, you would have to either:
- `int i2 = 2*i*i + 3*i + (i % 2);`
So you have to type this huge expression twice!
- write a function that does:
2*i*i + 3*i + (i % 2);
This function is almost useless (used only twice)
adding needless boilerplate to your code.
both of which are not very attractive alternatives.
*/
}
return EXIT_SUCCESS;
}