1. Iteration in Pseudocode
Iteration allows a program to repeat a set of instructions until a condition is met. Loops are fundamental tools for automating repetitive tasks, whether it’s calculating sums, processing arrays, or running through user inputs. In pseudocode, there are three primary types of loops: FOR, WHILE, and REPEAT. Each serves different use cases, and understanding when to use each is key to writing efficient code.
This guide covers these loops step-by-step, from simple examples to more complex scenarios involving options like stepping through ranges or using nested loops.
2. FOR Loop
A FOR loop is a count-controlled loop, meaning it runs a set number of times, typically iterating through a range of values. This makes it perfect for scenarios where you know in advance how many times you want the loop to run.
2.1. Simple FOR Loop
In its simplest form, a FOR loop repeats the code inside it a fixed number of times.
Syntax:
- Identifier: The loop variable, typically an integer.
- Start and End: The values at which the loop starts and ends.
Example:
This loop runs five times, outputting the current value of i
for each iteration.
2.2. FOR Loop with Step (Increments)
The FOR loop can be customized to increment by a value other than 1 using the STEP
keyword. This allows you to control how the loop progresses through the range.
Syntax:
Example:
In this example, the loop starts at 1 and increments by 2, so it outputs the values 1, 3, 5, 7, and 9.
2.3. Nested FOR Loops
You can nest one FOR loop inside another to handle multiple dimensions, such as iterating through the rows and columns of a grid.
Example:
This loop iterates over a 3x3 grid, outputting each cell's coordinates.
3. WHILE Loop
A WHILE loop is a condition-controlled loop, meaning it continues to execute as long as the condition remains TRUE. It’s useful when you don’t know in advance how many times the loop should run, but you know the stopping condition.
3.1. Simple WHILE Loop
In a simple WHILE loop, the condition is checked at the start of each iteration. If the condition is TRUE, the loop runs; if it’s FALSE, the loop stops.
Syntax:
Example:
In this example, the loop runs while x
is less than or equal to 5. After each iteration, x
is incremented by 1.
3.2. WHILE with Complex Condition
The condition in a WHILE loop can be as simple or complex as needed, including multiple variables and logical operators.
Example:
Here, the loop runs as long as both x < y
and y > 5
are TRUE. The loop exits when either condition becomes FALSE.
3.3. Nested WHILE Loops
Just like FOR loops, you can nest WHILE loops to handle more complex, multi-level iterations.
Example:
In this example, for each iteration of i
, the inner WHILE loop runs for j
, outputting pairs of i
and j
.
4. REPEAT Loop
A REPEAT loop (also called a DO...UNTIL loop) is another condition-controlled loop, but it differs from the WHILE loop in that it always runs at least once. The condition is checked after the loop body is executed.
4.1. Simple REPEAT Loop
The REPEAT loop executes the statements inside it and only checks the condition at the end. If the condition is FALSE, the loop runs again.
Syntax:
Example:
Here, the loop starts from 0 and keeps printing count until it exceeds 10
More contextual example:
Here, the loop keeps asking the user for a password until the correct one ("secret"
) is entered. The loop will run at least once, regardless of the initial value of password
.
4.2. REPEAT Loop with Complex Conditions
Just like the WHILE loop, you can use multiple conditions in a REPEAT loop to control when it stops.
Example:
Here, the loop keeps asking the user for a password until the correct one ("secret"
) is entered. However, this also keeps track of tries left to ensure loop ends after 3 tries, even if password was not guessed correctly.
4.3. Nested REPEAT Loops
You can also nest REPEAT loops, just like the other loop types, to handle more complex iterative tasks.
Example:
In this nested REPEAT loop, both loops run until their conditions are met, producing pairs of i
and j
.
5. Summary of Loops
Basic Syntax:
- FOR Loop:Loading...
- WHILE Loop:Loading...
- REPEAT Loop:Loading...
When to Use Each Loop:
- FOR Loop: Use when you know the exact number of iterations. Ideal for counting and processing arrays or collections of known size.
Examples: Iterating over a list of students, summing a fixed number of values, etc. - WHILE Loop: Use when the number of iterations is unknown but controlled by a condition that is checked before each iteration. It’s perfect for situations where you want to stop as soon as a certain condition is no longer met.
Examples: Waiting for user input until a valid choice is made, looping through a file until the end, etc. - REPEAT Loop: Use when you need the loop to execute at least once, regardless of the condition. The condition is checked after each iteration, making it suitable for user input loops where the prompt must appear at least once.
Examples: Prompting the user for a password, running an action that requires at least one execution, etc.
Conclusion
Iteration is a fundamental concept in programming, and choosing the right type of loop for the task at hand can make your code more efficient and readable.
- FOR loops: are best when the number of iterations is known in advance.
- WHILE loops: excel in situations where the stopping condition might change dynamically during execution.
- REPEAT loops: are perfect when you need to ensure that a block of code runs at least once.
Understanding and mastering these loops will give you the flexibility to write more dynamic and efficient pseudocode!