bbb11.jpg

Back 

Understanding and using iteration questions and answers

Q1. State the three types of iterative construct.
A1. For, While <cond> do, Repeat until <cond>
Q2. When should FOR be used? Give an example.
A2. Whenever you need to do a block of code a fixed number of times e.g. you need to calculate the average temperature in a month, for each month in a year. As there are twelve months, this code to work out the average is fixed – it will always be 12 and so a FOR loop should be used.
Q3. What is the key difference between the WHILE <cond> DO loop and the REPEAT UNTIL <cond> loop?
A3. WHILE <cond> DO may result in a block of code never actually being run! This is because the test is at the beginning of the construct. On the other hand, with REPEAT UNTIL <cond>, the test is at the end of the block of code. That means that the code will always be executed at least once. 
Q4. Why is indentation used in programming?
A4. Indentation makes code a lot easier to read. You can also see the start and finish of onstructions such as iterative constructions.
Q5. Indentation is very important in Python. What happens when you try to run a Python program when the indentation isn't perfect?
A5. You will get a syntax error if the indentation isn't perfect in Python. This is a good thing because it forces students to write well laid out code from the beginning of their learning journey.
Q6. What potential values does any Boolean expression result in?
A6. The only possible outcomes are TRUE or FALSE.
Q7. Is this TRUE or FALSE:     (34 <> 34) OR (14 < 6)   ?
A7. FALSE. Both expressions in brackets are FALSE.
Q8. Is this TRUE or FALSE:     (20 <> 14) AND (2 < 78)   ?
A8. TRUE. Both expressions in brackets are TRUE.
Q9. In Python, which of the three iterative keywords commonly found in traditional languages does it not have?
A9. It doesn't have REPEAT.
Q10. Why is this missing?
A10. You don't need it. REPEAT can be achieved another way in Python:

REPEAT
   ...........
   ..........
UNTIL <condition>

is the same as this in Python:

while True:
      .........
      .........
      if cond:
            break

Back