]> Pileus Git - ~andy/sfvlug/blob - c/src/jump.c
Add C notes.
[~andy/sfvlug] / c / src / jump.c
1 #include <stdio.h>
2
3 void bad_hello(int count)
4 {
5         int i = 0;
6 hello:
7         printf("Hello, %d!\n", i);
8         i++;
9         if (i < count)
10                 goto hello;
11 }
12
13 int ok_hello(int count)
14 {
15         if (count == 0)
16                 goto error_zero;
17         if (count < 0)
18                 goto error_neg;
19
20         for (int i = 0; i < count; i++)
21                 printf("Hello, %d!\n", i);
22
23         return 0;
24
25 error_zero:
26         printf("Count is zero!\n");
27         return 1;
28
29 error_neg:
30         printf("Count is negative!\n");
31         return 1;
32 }
33
34 int main()
35 {
36         bad_hello(3);
37         printf("\n");
38
39         ok_hello(1);
40         ok_hello(0);
41         ok_hello(-1);
42
43         return 0;
44 }