Sabine Beilfuss
Task 1:
Answer: The while loop prints 1, 2, 3 and excecutes the statement as long as the condition is true.
In other words it prints
the number 3, because the value of the variable loop_control was 2. And 2 incremented by 1 is 3.
In the next iteration of the loop the condition is false and the programme stops.
The for loop prints 0, 1, 2. It prints "0" first, because the variable loop_control is initialized within the loop and holds the value of zero in the first iteration. Adjustment is not performed at the first iteration. On the second iteration the condition is checked and the variable will be incremented by 1, the statement executed and 1 is displayed. etc.
Task 2:
Task 3:
Task 4:
Task 5: Review questions
1. A while loop executes the statement and than checks if the condition is true, before executing the statement another time.
The for loop checks if the condition is true before it executes the statement (adjustment). This has an influence how you set
the values for the condition.
2. To check if the temperature of the water is right, use a while loop, because you don't know how many times the loop has to be executed in order
to meet the condition temperature == 100 (degree celsius)
3. For this example a for loop is appropriate, because you know that the loop has to be executed 457 times, to find out if all the items
are there.
back to index
© Sabine Beilfuss, March 2003