-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path200-continue-eg.c
More file actions
52 lines (48 loc) · 932 Bytes
/
200-continue-eg.c
File metadata and controls
52 lines (48 loc) · 932 Bytes
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
long loop1()
{
/* $begin 200-continue-for-c */
/* Example of for loop using a continue statement */
/* Sum even numbers between 0 and 9 */
long sum = 0;
long i;
for (i = 0; i < 10; i++) {
if (i & 1)
continue;
sum += i;
}
/* $end 200-continue-for-c */
return sum;
}
long loop2()
{
/* $begin 200-continue-while-c */
/* Naive translation of for loop into while loop */
/* WARNING: This is buggy code */
long sum = 0;
long i = 0;
while (i < 10) {
if (i & 1)
/* This will cause an infinite loop */
continue;
sum += i;
i++;
}
/* $end 200-continue-while-c */
return sum;
}
long loop32()
{
/* $begin 200-continue-fix-while-c */
/* Correct translation of for loop into while loop */
long sum = 0;
long i = 0;
while (i < 10) {
if (i & 1)
goto update;
sum += i;
update:
i++;
}
/* $end 200-continue-fix-while-c */
return sum;
}