COMPSCI4039 Chap.6 Loops and the Range Function
Loops and the Range Function
Loops and the Range Function introduces repetition, the building block that lets a few lines process any number of items. A while loop tests its condition before every pass and runs its body while the condition holds, so something in the body must eventually make the condition false; otherwise the loop never ends, and Ctrl and C is needed to stop it.
Counter loops are common enough to have their own form: a for loop walks through a sequence, and range produces sequences of evenly spaced integers from a start up to, but not including, a stop, with an optional step that may be negative. A for loop can also run through the items of any list.
Python has no do-while loop, so the course imitates one with while True and a break at the end of the body, which guarantees at least one pass; continue skips the rest of the current pass. Choosing between loops depends on what is known when the loop begins: a known count suits for, while an unknown count that depends on incoming data suits while.
Nested loops and prime-number programs show how larger problems break into smaller loops.
What this chapter covers
- 01
Why repetition replaces copied lines
- 02
While loops and continuing conditions
- 03
Infinite loops and how to avoid them
- 04
For loops over range and over lists
- 05
Range with start, stop and step
- 06
Simulated do-while loops with break and continue
- 07
Choosing between while and for
- 08
Nested loops and problem decomposition
Worked example · free
Sum the multiples of 3 below 20 with range
- 1range(3, 20, 3) starts at 3 and adds 3 each time. The allocation shown is an original practice weighting and not a university assessment scheme.
- 1The values are 3, 6, 9, 12, 15 and 18; 21 would pass the stop value, and 20 itself is never produced.
- 1Each pass adds the current value to total.
- 13 + 6 + 9 + 12 + 15 + 18 is 63.
Key terms
- Infinite loop
- A loop whose condition never becomes false, so it repeats until the program is stopped.
- Loop body
- The indented block of statements that a loop runs on each pass.
- Break statement
- A statement that ends the enclosing loop immediately.
- Sentinel value
- A special input, such as -1 or 0, that signals the end of a sequence of data.
Loops and the Range Function FAQ
Why does range(1, 10) stop at 9?
The stop argument of range is always excluded, so range(1, 10) produces 1 to 9. To include 10 you would write range(1, 11). This exclusion is the most common source of off-by-one answers in loop questions.
When should I use while instead of for?
Use for when the number of repetitions is known as the loop starts, such as stepping through a list or a fixed range. Use while when repetition depends on data that arrives during the loop, such as reading values until the user types a stop value.
How do I make a loop run at least once?
Python has no do-while loop, so write while True and place a test with break at the end of the body. The body always runs once before the test is reached, which the examples class contrasts with a plain while loop that may run zero times.
How do I count passes in a nested loop?
The inner loop runs completely on every pass of the outer loop. Add up the inner loop's length for each outer value. If the inner range depends on the outer counter, the total is a sum, not a simple product.
What do break and continue do?
break ends the loop at once, and execution continues with the first line after the loop. continue abandons the rest of the current pass and jumps back to the next test or item. The course uses break to imitate a do-while loop; continue is rarer, and a well-chosen condition often makes it unnecessary.
Exam move
Build loop fluency with three kinds of drill. First, range listing: for a dozen range calls with one, two and three arguments, including negative steps, write the exact sequence produced and circle the stop value that is missing.
Second, hand tracing: for each while loop you meet, make a table with the condition, the variables and the output for every pass, and identify the line that eventually makes the condition false; if you cannot find one, the loop is infinite.
Third, rewriting: take a for loop and rewrite it as a while loop with an explicit counter, then as a simulated do-while loop, and confirm that all three versions give the same results, as the examples sheet does with factorials. For problem solving, follow the lecture's advice on primes: break the task into a function that tests one number and a loop that applies it to every candidate.
Before December, practise the choice question directly: given a short scenario, decide whether the number of passes is known when the loop begins and pick the loop accordingly.
Working through Loops and the Range Function in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Loops and the Range Function question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.