Skip to content

Commit 056b7ad

Browse files
author
Matthew Gibbons
committed
Loops Tutorial Fleshing
Removed whitespace from answers and practice. Expanded beyond the introduction of the loops tutorial. While added. Do while added. For started. I remember there are some weird things you can do with for loops that allow you to have no parameters but use it as a proper for loop.
1 parent be1909c commit 056b7ad

3 files changed

Lines changed: 136 additions & 3 deletions

File tree

src/intro-to-c/loops/loop-practice-answers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ toc: false
376376
\newpage
377377
378378
@. **Challenge:** Write a program that prints a diamond shape with a height of `n`. Assume that `n` is greater than 2 and is odd. As an example, the following is a diamond with a height of 5.
379-
379+
380380
```
381381
#
382382
# #

src/intro-to-c/loops/loop-practice.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ toc: false
183183
```
184184

185185
@. **Challenge:** Write a program that prints a diamond shape with a height of `n`. Assume that `n` is greater than 2 and is odd. As an example, the following is a diamond with a height of 5.
186-
186+
187187
```
188188
#
189189
# #

src/intro-to-c/loops/loops.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,137 @@ author: Matthew Gibbons
55

66
## Intro to Loops
77

8-
All good roller coasters have loops. Unlike roller coasters, not all good programs have loops. However, programs that have repeated code have loops instead, and that makes them good.
8+
<!-- **TODO:** Infinite loops, one-line for loops, nested loops. -->
9+
10+
All good roller coasters have loops. Unlike roller coasters, not all good programs have loops. However, programs that have repeated code have loops instead, and that makes them good. In C, there are three kinds of loops we can use: for loops, while loops, and do while loops. We use loops to rerun some lines of code based on a condition.
11+
12+
### While Loops
13+
14+
While loop syntax looks like this:
15+
16+
``` c
17+
while(condition)
18+
{
19+
//code to execute
20+
}
21+
```
22+
23+
As long as the condition is true, the loop will continue to run. Any nonzero integer is true in C.
24+
25+
An example of a while loop would look something like this:
26+
27+
``` c
28+
int x = 15;
29+
30+
while(x >= 5)
31+
{
32+
printf("X is %d.\n", &x);
33+
x--;
34+
}
35+
36+
printf("I'm free of this loop.");
37+
```
38+
39+
This loop would print give us this:
40+
41+
```
42+
X is 15.
43+
X is 14.
44+
X is 13.
45+
X is 12.
46+
...
47+
X is 6.
48+
X is 5.
49+
I'm free of this loop.
50+
```
51+
52+
5 is included because of the condition being a `>=` comparison. You may also notice the decrement of `x`. While loops are easy to make into infinite loops. Without the decrementing `x`, this loop would be infinite because the condition would never become false.
53+
54+
A quick example of this, using the nonzero-is-true property of C, could be something like this:
55+
56+
``` c
57+
x = 1;
58+
59+
while(0 - x)
60+
{
61+
printf("Oh no.\n");
62+
x++;
63+
}
64+
```
65+
66+
`0 - x` will only become more negative and never reach zero (neglecting integer overflow) and thus it will be an infinite loop. We could just as easily remove the `x++` to do the same thing, and then not even integer overflow could save us.
67+
68+
All loops can have multiple lines of code in them, these ones have just been easy examples. They can also have multiple conditions if you use logical operators.
69+
70+
``` c
71+
while(control == 1 || control == 3)
72+
{
73+
if(control == 1)
74+
//move forward
75+
else
76+
//move backwards
77+
scanf("%d", control);
78+
}
79+
```
80+
81+
### Do While Loops
82+
83+
Do While loops are used when there is some amount of code you want to execute before the while loop checks the condition. Do whiles check the condition at the end of the loop, rather than at the beginning. It would be similar to a friend holding a small pebble thinking it was a peanut, and claiming that it was before trying to eat it.
84+
85+
That as a do while might look something like this:
86+
87+
``` c
88+
int peanut = 1;
89+
int rock = 0;
90+
int x = rock;
91+
92+
do
93+
{
94+
printf("This is a peanut.\n");
95+
}while(x != rock)
96+
97+
printf("It was a rock.\n")
98+
```
99+
100+
This do while will print `This is a peanut.` one time, because we know the peanut is a rock. What if we didn't know? What if he had a back of peanut shaped rocks and peanuts and grabbed one at random? Then we might get a loop that executes more than once, until they find a rock.
101+
102+
``` c
103+
#include <stdio.h>
104+
#include <stdlib.h>
105+
#include <time.h>
106+
107+
int main(void)
108+
{
109+
int peanut = 1
110+
int rock = 0
111+
112+
srand(time(NULL));
113+
int mystery_snack = rand() % 1;
114+
115+
do
116+
{
117+
printf("This is a peanut.\n");
118+
}while(x != rock)
119+
120+
printf("It was a rock.\n")
121+
return 0;
122+
}
123+
```
124+
125+
This is a very confident friend, as they always claim peanut and then accept the consequences.
126+
127+
### For Loops
128+
129+
For loops initialize a variable, check a condition, and increment (or decrement) in their declaration, separated by semicolons. It would look something like this:
130+
131+
``` c
132+
for(int x = 0; x < 25; x++)
133+
{
134+
printf("The loop has run %d times.\n", &x + 1);
135+
printf("X is currently %d.", &x);
136+
}
137+
```
138+
139+
`int x = 0` is the initialization, `x < 25` is the condition, and `x++` is the increment. The first time this loop runs, `x` will be 0 and will increment at the end of the loop.
140+
141+
For loops have some weird things you can do with them.

0 commit comments

Comments
 (0)