From 7ab9d75b405a3ab46bda5a232baa9e88f0c07d6a Mon Sep 17 00:00:00 2001 From: Erik Strand <erik.strand@cba.mit.edu> Date: Wed, 21 Oct 2020 00:09:09 -0400 Subject: [PATCH] Add a demo of different control structures --- 02_control_structures/Makefile | 19 +++++ 02_control_structures/loops.c | 72 +++++++++++++++++++ .../Makefile | 2 - .../README.md | 0 .../add_integers.c | 0 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 02_control_structures/Makefile create mode 100644 02_control_structures/loops.c rename {02_command_line_tool => 03_inputs_and_outputs}/Makefile (60%) rename {02_command_line_tool => 03_inputs_and_outputs}/README.md (100%) rename {02_command_line_tool => 03_inputs_and_outputs}/add_integers.c (100%) diff --git a/02_control_structures/Makefile b/02_control_structures/Makefile new file mode 100644 index 0000000..1d0e24c --- /dev/null +++ b/02_control_structures/Makefile @@ -0,0 +1,19 @@ +# 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 +clean: + rm loops diff --git a/02_control_structures/loops.c b/02_control_structures/loops.c new file mode 100644 index 0000000..2e9710d --- /dev/null +++ b/02_control_structures/loops.c @@ -0,0 +1,72 @@ +#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". + ++i; + } + + // 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"); + break; + } + + ++i; + } + + // 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) { + continue; + } + + // 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"); + break; + case 1: + printf("i is 1\n"); + break; + case 2: + printf("i is 2\n"); + break; + case 42: + printf("i is 42\n"); + break; + default: + printf("i is something else\n"); + } +} diff --git a/02_command_line_tool/Makefile b/03_inputs_and_outputs/Makefile similarity index 60% rename from 02_command_line_tool/Makefile rename to 03_inputs_and_outputs/Makefile index 34690ff..f682fac 100644 --- a/02_command_line_tool/Makefile +++ b/03_inputs_and_outputs/Makefile @@ -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 clean: rm add_integers diff --git a/02_command_line_tool/README.md b/03_inputs_and_outputs/README.md similarity index 100% rename from 02_command_line_tool/README.md rename to 03_inputs_and_outputs/README.md diff --git a/02_command_line_tool/add_integers.c b/03_inputs_and_outputs/add_integers.c similarity index 100% rename from 02_command_line_tool/add_integers.c rename to 03_inputs_and_outputs/add_integers.c -- GitLab