04 Getting Looped

Hello again! Today we dive into loops. Loops are essential to programming
because they save typing out squillions of lines of code. Let's take a look at
this short example:

============

// loopy, getting looped in C!

#include <stdio.h>

int main ()
{
int a = 0;
int total = 0;

for ( a = 1; a <= 50; a = a + 1 )
total = total + a;
printf ( "The sum of this loop is %i\n", total );
return 0;
}

============

This results in:

$ loopy
The sum of this loop is 1275

Let's add a few lines that expand the loop so we can see what it is doing:

============

#include <stdio.h>

int main ()
{
int a = 0;
int total = 0;
int longway = 0;

for ( a = 1; a <= 50; a = a + 1 )
total = total + a;
printf ( "The sum of this loop is %i\n", total );

longway =
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;

printf ("The sum of adding the loop the long way is %i\n", longway );
return 0;
}

===========

$ loopy
The sum of this loop is 1275
The sum of adding the loop the long way is 1275

You can play with this by changing the values of a in the loop statement, like
this:

for ( a = 5; a <= 33; a = a + 3 )

Let's dissect this so we know what the heck is going on. We start with our
standard comment with the program name, and then the preprocessor statement:

// loopy, getting looped in C!

#include <stdio.h>

Then our main function, which is required in C programs:

int main ()
{

Then we declare and initialize our variables. The declaration is 'int a', and
initializing means assigning it a value, like'int a = 0'. We already learned
that initializing variables when they are declared is a good practice for
preventing unpredictable results.

int a = 0;
int total = 0;

In the olden days before the C99 standard it was a requirement to declare
variables at the beginning of a statement block (the code between pairs of
curly braces.) C99 allows us to mix declarations and statements in whatever
chaotic ways we want to. For the sake of readability and maintainability let's
stick with the old way.

Now we come to the engine of this little program, the for statement.

for ( a = 1; a <= 50; a = a + 1 )

The basic structure of a for statement is like this:

for ( variable_initialization; loop_condition; loop_expression )
code that executes as long as the condition is true, our program
statement

variable_initialization, loop_condition, and loop_expression are not any kind
of official terminology; they are descriptive terms that I made up.

So 'a = 1' initializes a to a value of 1, its value before the loop begins to
run.

'a <= 50' means "keep running as long as a is less than or equal to 50." You
could also say 'a < 51'.

'a = a + 1' performs the calculations of the for statement, and the results of
these calculations are tested against the loop_condition. As long as the test
returns True, which means 'a = a +1' is less than or equal to 50, the program
keeps running. When it returns False, which is 51 or greater, then the program
stops.

One way of capturing the results of the loop is with our program statement:

total = total + a;

This passes the value of 'a' into 'total', and then we print it in the way we
learned in our previous lessons:

printf ( "The sum of this loop is %i\n", total );

But that only prints the final result. We can see the progress of the loop by
printing 'a' directly:

printf ( "The sum of this loop is %i\n", a );

So this example:

========

#include <stdio.h>

int main ()
{
int a = 0;

for ( a = 12; a <= 30; a = a + 4 )
printf ( "The sum of this loop is %i\n", a );
return 0;
}

=========

Does this:

$ loopy
The sum of this loop is 12
The sum of this loop is 16
The sum of this loop is 20
The sum of this loop is 24
The sum of this loop is 28

So if you're a little wobbly with your arithmetic and have to work things out
the long way, this helps you see what your program is doing.

printf has many wonderful formatting options that we can take advantage of. I
place a high value on readability. So we can make our little example look all
cool:

=================

#include <stdio.h>

int main ()
{

int a = 0;

printf ( "This is my awesome loopy program\n\n" );
printf ( " See the value of a in each\n loop in a nice table\n" );
printf ( "________ \n" );

for ( a = 12; a <= 30; a = a + 4 )
printf ( " %i\n",a );
return 0;
}

==================

And it produces this:

$ loopy
This is my awesome loopy program

See the value of a in each
loop in a nice table
________
12
16
20
24
28

Note how spacing and line breaks are handled.

HOMEWORK

Modify the last example to display two columns of output that use the 'a' and
'total' variables. Play around with spacing and line breaks.

Make a box around your program, like

+++++++++++++++++++++++++++++
+This is my awesome loopy program +
+ +
+ See the value of a in each +
+ loop in a nice table +
+________ +
+ 12 +
+ 16 +
+ 20 +
+ 24 +
+ 28 +
++++++++++++++++++++++++++++++

Next week we shall take a deep dive into functions. We've been using them all
along, so it's high time to start understanding them in more depth.