Loops theory + first problem: print every port from 8000 to 8010
Problem statement
A loop repeats a block of instructions without you writing it out by hand each time. Instead of eleven separate print lines for ports 8000 through 8010, a loop runs one print line eleven times, changing a variable each time through.
Common mistake: an off-by-one error, stopping one short of the last value.
Wrong (Python): for port in range(8000, 8010):Python's range(8000, 8010) stops before 8010, so it prints up to 8009 only. To include 8010, the range needs to end one past it: range(8000, 8011). Java and C++ don't have this specific trap the same way, since their loop condition is written directly (port <= 8010), but it's worth double-checking either way.
Your task: print every port number from 8000 to 8010, inclusive, one per line.
Approach
Each language counts up with a loop, the syntax differs but the idea is identical: start at 8000, keep going while the value is at most 8010, print, then move to the next number.
- Python's
for port in range(8000, 8011):handles the counting automatically; remember the range's second number is exclusive, so it needs to be one past the last value you want. - Java and C++ use a
forloop with all three parts spelled out: the starting value, the condition to keep looping, and how to move to the next value.
for port in range(8000, 8011):
print(port)