5 – Loops

With loops we can do a lot of interesting things, and we will be looking at all of the different types of loops in this lesson. Loops are pieces of code that get executed over and over until a certain criteria is met or the loop is exited.

A QUICK WORD OF CAUTION pay close attention to the code you write when doing loops, as you could potentially end up freezing your computer. If this happens, you’ve made an infinite loop, and depending on your operating system, it may render your computer unresponsive and you’ll have to restart. This doesn’t necessarily harm your computer, but you will lose any unsaved data.

While …

The first loop I want to introduce you to, is the while loop. This loop is probably the simplest to understand and its concept applies to the other types of loops.

A while loop is defined by the keyword while followed by a statement in parentheses and a code-block to be executed if the statement is true, as shown below.

int i = 0;

while ( i < 100 )
{
	// while the statement is true, this code-block will be executed.
	printf(”I will never throw paper planes in the classroom again.\n”);
	
	// increment the variable i
	i++;
}

Each time the code-block reaches the end, it will go back and check if the statement is still true. And as you will notice from the example above, we are checking whether or not the variable ‘i’ is less than 100. We start of with ‘i’ being 0 and on each pass where the while loop evaluates this to true, we increment the variable ‘i’ — obviously ‘i’ will eventually reach 100, in which case the while loop will evaluate false and exit the loop.

Do … While …

The do-while is almost the same as the while loop, with one difference, and that is that this loop is guaranteed to execute its code-block at least once — which is very handy in some cases.

We define a do-while loop with the keyword do followed by its code-block, ending it with a while statement, as shown below.

int i = 0;

do
{
	// while the statement is true, this code-block will be executed.
	printf(”I will never throw paper planes in the classroom again.\n”);
	
	// increment the variable i
	i++;
}
while ( i < 100 );

Take note that the do-while is terminated (semi-colon).

For …

The for-loop is pretty much an expanded version of the while-loop, feature-wise. It allows us to execute an instruction before we enter the loop and one to execute after each pass, along with the statement of course.

We define a for-loop by the keyword for followed by a parenthesis, inside this parenthesis we put the instruction to be executed immediately before we enter the loop, a statement and the instruction to be executed on each ended pass. And finally, we end it with its code-block, of course. A for loop definition is shown below.

for ( int i = 0 ; i < 100 ; i++ )
{
	// while the statement is true, this code-block will be executed.
	printf(”I will never throw paper planes in the classroom again.\n”);
}

As you can see, the for-loop is just a condensed version of the while-loop, and is quite simple even though it might look intimidating at first.

Previous: Conditionals
Next up: Functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s