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:
Concepts:Loop structures are used to control the flow of a program. Their are three forms of program flow:
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:
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. ArraysAn 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. FunctionsFinally, 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:
Students should clearly understand the difference between calling, declaring and defining a function.
|