C LAB WORKSHEET 7
The C & C++ Repetition: The for Loop 1
Items in this page:
In this practice we will concentrate on the program controls specifically loop constructs that used in C/C++ programming. We will use flowchart to assist us in understanding the program controls.
Instead of a sequence of C/C++’s code execution in programs, in the real C/C++ programming there is a lot of conditional and branching. Other than a sequence code execution, a common loops (repetition) in C/C++ programming is the for,while and do-while loops. For conditional execution (selection) we can use if and its variations such as if-else, if-else-if and switch-case-break. Other constructs for conditional compilation includes using goto and exit however in this worksheet we will play around with for loop and its friends.
Flowchart and tracechart
Flowchart shows the logic of a program graphically and optionally, tracechart shows the sequence of steps taken to execute the flowchart. Let have a C pseudo code for simple integer iteration program example.
Declare an integer variable, j and set an initial value to -4,j = -4.
Check the condition for j,j <= 0 true or false?
Print the j’s value.
Increment j by 1.
Repeat the iteration process untilj <= 0 is false.
If the condition j<=0 is false, stop exit the loop and continue the execution of the next code if any.
In a simple word we are going to print an integer number from -4 to 0. From the pseudocode, let translate it to a flowchart, you will see a better C program logic. The loop (usingfor construct in this example) is depicted by the dash red line and it is not part of the flowchart.
Notice that if only one statement after thefor statement, then a set of braces is not required. They are needed only if more than one statements in the loop. Generally, the for statement will have the following form.
for(initial value; condition; iteration)
Remember there is no semicolon at the end of the for statement. Try the following example and show the output.
In this example, we first obtain the user’s name and save it in name[ ]. Then we obtain the number of times the name to be displayed. In the sample run shown, this number is 10 and it is stored in variable count. Then the loop begins. From the program we can construct the flowchart as shown below.
The terminal condition is evaluated, count != 0? (is count not equal to 0?) is true, since count is equal to 10, then the body of the loop is executed. Since only one statement is in the body of the loop, no set of braces is required and the name is printed.
Now we go up to the for statement and assign count to be 1 (10 – 1 = 9) less than what it was. Then evaluate again, count != 0? is still true so the loop is performed, that is the name is printed a second time. The iteration of the loop is perform until count != 0? is false, meanscount = 0 and the loop is stopped. Take note that the loop is stopped not the program itself if there are more codes after the loop body, the execution will continue.
From the previous examples you can se that we can write down the C program from the flowchart and vice versa, we also can create a flowchart from C program. Typically we start designing our C program from scratch using pseudocode, and then we build the flowchart and finally write the real C codes. Optionally we can build the tracechart based on the flowchart for the loop part of the program to see the detail of the loop execution.
Run the following program, show the output and answer the question.
Try the following program.
Run the following program.
Next, let simplify the previous program. Run and show the output.
The for loop now shows three expressions. We will call the first expression as i = 0, the second expression i <= 4, and the third expression is i = i + 2.
Add a semicolon at the end of the for and watch what happens.
Next, let try making i go backward (decrement) by changing <= to >= operator.
What about the not equal to operator,!= as shown below.
Theprintf("%d ", i); statement will be executed except when i = = 2.
What happen to the following program output? How to stop it? How would you correct it?
The output of this program will be infinite number because the for loop doesn't have a terminal condition. The output is 'overflow'. The i != 2 expression is always true in this case. To stop it press Ctrl + C.
We need to provide a terminal condition for the loop. In this case we can change the i = i + 1 to i = i - 1 to provide a terminal condition. So we need to provide a terminal condition in the for statement else the for loop won't terminate.
Compress or simplify the following code as much as possible by retaining the output.
The program seems can't be simplified anymore. In this program i has been initialized to 0. Then the for statement only provide i < 5 condition expression. The for loop statement that has been executed indefinitely is printf("%d ", i);. In this program, the i < 5 is always true because there is no iteration (decrement or increment) to make it closer to the terminal value, i < 5. The for loop run continuously while printing the initial value of i = 0.