UNIX C Programming

Chapter 6: C Control Statements: Looping

 

Objectives:

This chapter continues the discussion of looping. Besides the while loop, the for and do while loops are examined. In addition, relational operators and their use are considered. The chapter concludes with an introduction to arrays. The teaching objectives important to this chapter include:

  • Provide several examples using the while, for, and do while loop structures
  • Show how relational operators are used to control loop execution
  • Demonstrate how to break from a loop
  • Explain the concept of an array
  • Examine functions which have return values
Concepts:

Loop structures are used to control the flow of a program. Their are three forms of program flow:

  1. Executing a sequence of statements
  2. Repeating a sequence of statements until some condition is met (looping)
  3. Using a test to decide between alternative sequences (branching)

Pseudocode is the art of expressing a program in simple English that parallels the form of a computer language.

The three types of loop structures are the while loop, the for loop, and the do-while loop.

The general form of the while loop is:

	while (expression) 
	statement; 

The while loop continues to execute until the expression becomes false (or zero).

The general form of the for loop is:

	for (initial; test; update) 
		statement; 

This type of loop continues to execute until the test becomes false (or zero).

A for loop may be repeated for a fixed number of times, although this is not necessary. When setting up this loop, three actions are involved:

  1. A counter must be initialized (initial).
  2. The computer must be compared with some limiting value (test).
  3. The counter must be incremented each time the loop is traversed (update).

The general form of the do-while loop is:

	do 
	statement; 
	while (expression); 

This loop also continue to execute until the expression becomes false (or zero).

Both the while and the for loops use an entry condition. This test condition is evaluated before the loop is entered. The do while loop features an exit condition. The loop body must execute at least once, after which time the test condition is evaluated.

Relational operators are used extensively in loop structures to determine if a test condition is true or false. The relational operators and their meaning are:

Operator Meaning

	<	Is less than 
	<=	Is less than or equal to 
	==	Is equal to 
	>=	Is greater than or equal to 
	>	Is greater than 
	!=	Is not equal to 

In C, if a relational test is found to be true, a value of 1 is returned. If found to be false, a value of 0 is returned.

Relational operators have a lower precedence than arithmetic operators and divided into a high and low priority group. These groups are:

high-priority group < <= > >=

low-priority group == !=

The comma (,) operator is used to extend the flexibility of the for loop. It allows the general form to be modified as follows:

for (initial_1, initial_2 ; test; update_1, update_2) 
statement; 

While not restricted to for loops, the comma operator is used most often with this type of loop structure.

Nested loops consist of one loop placed inside another loop. An example of a nested for loop is:

	for (initial, test, update) 
	{ 
		for(initial, test, update) 
			statement; 
		statement; 
	} 

In this example, the inner loop runs through its full range of iterations for each single iteration of the outer loop.

Arrays

An array is a series of values of the same type, such as:

	float debts[20]; 

The numbers used to identify array elements are called subscripts, indices, or offsets. They must be integers and all subscripting begins with zero. Thus, the first element of the debts array shown above is debts[0].

The different types of loop structures encourage the concept of modularity: the dividing of a program into units, with each unit having one task to perform.

Functions

Finally, the last subject in this chapter examines the return of a value from a function.

To write a function with a return value do the following:

  1. When you define the function, state the type of value it returns.
  2. Use the keyword return to indicate the value to be returned.

Students should clearly understand the difference between calling, declaring and defining a function.

  • A function is called by using its name in a program, passing whatever arguments it requires to it in parentheses.

  • A function is declared by the function prototype. The declaration ends with a semicolon.

  • A function is defined by writing the function heading. No semicolon is required.

  • Declaring the function before it is defined is known as a forward declaration. This is standard.