Commit 7ab9d75b authored by Erik Strand's avatar Erik Strand
Browse files

Add a demo of different control structures

parent 6efdccd2
# Change your compiler here if you're not using gcc. CC is for C, and CXX is for C++.
CC = gcc
CXX = g++
CFLAGS = -Wall -O3
.PHONY: all
all: loops
# Here I'm using some of Make's built in variables: $@ and $<. The former gets substituted with the
# name of the target. In this case, that's "loops". The latter gets substituted with the first
# prerequisite. In this case, that's "loops.c". These are hard to remember, but can save you some
# typing if you decide to rename things.
loops: loops.c
$(CC) $(CFLAGS) -o $@ $<
.PHONY: clean
rm loops
#include <stdio.h>
int main(void) {
// Here we declare an integer called i, and set it equal to zero.
int i = 0;
// This is a while loop. The code in the braces will run repeatedly as long as the condition is
// true. You don't actually need braces if there's only one statement (i.e. line). But it's not
// a bad habit to write them anyway, so that you don't accidentally skip anything. Unlike in
// Python, the indentation doesn't mean anything to the compiler -- it's just for you.
while (i < 5) {
printf("while loop: %i\n", i);
// This increments i. We could also use "i = i + 1" or "i += 1".
// This is a for loop. It's like a while loop, but you name the iteration variable, state the
// loop condition, and write your increment statement (or whatever else you want to do at the
// end of each iteration) all up front.
for (int j = 0; j < 5; ++j) {
printf("for loop: %i\n", j);
// In C, 0 means false, but any other integer means true (usually we use 1). So this loop would
// run forever, except we we have a trick up our sleeve: you can exit the loop with the keyword
// "break". This works in for loops too.
i = 0;
while (1) {
printf("another while loop: %i\n", i);
if (i == 2) {
printf("leaving the while loop\n");
// Another useful loop keyword is "continue". It jumps to the next loop iteration early. Like
// "break", it works in for loops and while loops. Also note that we can reuse i here, like
// we've been doing in our while loops. Both loops work both ways, but it's more common to see
// for loops declare new variables.
for (i = 0; i < 5; ++i) {
// Here we use the modulo operator "%" to check if i is even.
if (i % 2 == 0) {
// This code only runs if i is odd.
printf("%i is odd\n", i);
// This is a switch statement. It's not a loop, but it is handy to dispatch to different pieces
// of code based on the value of some variable.
i = 42;
switch (i) {
case 0:
printf("i is 0\n");
case 1:
printf("i is 1\n");
case 2:
printf("i is 2\n");
case 42:
printf("i is 42\n");
printf("i is something else\n");
......@@ -10,8 +10,6 @@ all: add_integers
add_integers: add_integers.c
$(CC) $(CFLAGS) -o add_integers add_integers.c
# This rule deletes all the binaries. It's declared "phony" which means it doesn't actually make
# anything. Otherwise Make would get confused about when it should run this rule.
.PHONY: clean
rm add_integers
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment