Subtopic Notes

F - Iteration/Loops

Preq. Programming Basics

Iteration/Loops

Iteration is used when you want to execute a block of code repeatedly. The code is repeated for a specified number of times or until a condition is met.

Count-Controlled Iteration

The code is repeated a fixed number of times. You should use it when you know exactly how many times a block will loop. It clearly expresses the variable initialization, condition and increment in one line (Best for incrementing through an array).

range(start, stop, step)

  • start (optional):
    • Starting value of the sequence.
    • Inclusive
    • Default Value 0
  • stop (required):
    • The endpoint of the sequence
    • Exclusive
  • step (optional):
    • The difference between each number in the sequence
    • Defaults Value: 1

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Pre-Conditioned Loops

  • The condition is checked before each iteration
  • The loop executes only if the condition is true.
  • May execute zero times if the condition is false initially
  • Useful for indefinite loops or when the condition may initially be false

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Post-Conditioned Loops

  • Condition is checked after the block of code executes
  • Executes at least once regardless of the condition
  • Not available in python

PSEUDOCODE

Pseudocode uses basic Cambridge-style keyword highlighting. Execution is not supported yet.

Editor — Pseudocode

Infinite Loops: A loop that continues to execute endlessly because its termination condition is never met or there is no condition to stop it. It might cause the program to freeze so always make sure to change values of variables when needed. Some infinite loops might be intentional (eg. waiting for an input)

PYTHON

Python can be executed directly in the browser.

Editor — Python
Output
No output yet.

Nested Statements

  • Different types of constructs can be placed one inside another using indentation
  • It helps to minimize code duplication and makes testing easier