Freelance Writing Jobs | Today's Articles | Sign In

 

Using Loops in C Programming

How to identify and maintain stored, retrieved, and updated variables.

Apr 14, 2006 Guy Lecky-Thompson

This tutorial teaches the use of loops in C/C++ programming, and is aimed at beginner to intermediate programmers. It covers counted (for) and condition tested (while and

Introduction

In this C programming tutorial, we are going to learn how to execute a set of statements repeatedly in a controlled fashion. If the reader has not yet set up their environment using the Borland C compiler (or equivalent) they should refer to C Tutorial 1 before reading the rest of this tutorial.

We also introduce the concept of variables, albeit gently, in this tutorial. A variable can be seen as a named box into which information can be:

  • Stored;
  • Retrieved;
  • Updated.

It is temporary storage space of a given size and type, and a language-neutral discussion of data types and variables can be found here:

These tutorials are also part of the Computer Programming Topic here at Suite101.

The for Loop

A for loop could look like the following:

int nCounter; // Declare a number variable
for (nCounter = 0; nCounter 10; nCounter++)
{
// ... code statements ...
}

As is usual with the C programming language, the code block to be executed is contained between braces ( { and } ), and in this case will be executed 10 times.

This condition is specified in parentheses in the for statement line:

for ( ; ; )

The end_value_test (condition expression), and the update_operation (expression) must follow the rules for the data type being used as a status variable. Other than that, these expressions can be any valid C statement. For example, we can loop through the alphabet:

char cLetter; // A character value
for (cLetter = 'a'; c = 'z'; c++)
{
// ... code statements ...
}

(We could equally write c = c + 1 as the update_operation expression, which is more in keeping with other programming languages.)

The do & while Loops

Many novice programmers view do and while as equivalent ways to achieve the same effect, so we'll start by giving a code example for the traditional variant, and look afterwards at the more esoteric one.

do
{
// ... code statements ...
} while ();

The statements between braces are executed, until the expression in the while statement evaluates to true. The advantage over using a for loop is that the expression being tested can contain multiple variables to be evaluated, which can be changed as part of the loop execution, making for a more flexible approach.

The variant of this is:

while ()
{
// ... code statements ...
}

It may look very similar, but there is an important difference - the condition is tested before the code statements are executed for the step of the loop in question. Sometimes this can be useful, but sometimes it leads to errors, so should be used with care.

The break & continue Keywords

These two keywords allow us to control the looping behaviour of our C program, sometimes in a way which alters the behaviour subtly enough that bugs can be introduced without the programmer catching them during development.

However, they do play a part in more advanced programming techniques, so it is worth introducing them at this point.

The break keyword allows the programmer to exit the loop prematurely; possibly as a result of detecting an error which would prevent the loop from terminating successfully.

The continue keyword returns execution to the next step in the loop, without executing the code that follows it; it can be used to skip some code execution that may not be relevant during the current cycle.

Questions

Test your knowledge, by considering the following questions:

  1. How would you print every 2nd letter of the alphabet?
  2. And in a while loop?
  3. Now do it using the continue keyword!

(Hint: for the last one, you'll need to use an if statement - look out next time for a description of condition testing.)

Answers via the mailing list, as well as an advance copy of the Lessons, as they arrive.

Back to Lesson 1 : Introduction..

The copyright of the article Using Loops in C Programming in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Using Loops in C Programming in print or online must be granted by the author in writing.
What do you think about this article?

NOTE: Because you are not a Suite101 member, your comment will be moderated before it is viewable.
post your comment
What is 1+4?
;