Loops in Pseudocode
So say you have just written a great program in pseudocode, but you are stuck! You are not 100% sure as to how iteration works in pseudocode, so that is where we help! Iteration can play a huge part in algorithms, whether you are making a quiz app, a calculator or even working with databases. But the thing is, iteration is much, much simpler than it seems!
In this guide we will be covering For loops in pseudocode, While loops in pseudocode and even Do loops in pseudocode. What can loops be used for in pseudocode? If you want to repeat a certain piece of code a certain number of times, that is exactly where loops help you. We will go into how to use each loop in pseudocode, what each loop is used for, and examples for all 3 types.
We follow the pseudocode standard set by the main computer science exam board in the UK, AQA, allowing a universal set of functions and operators to be used. This makes it much easier for programmers to understand each other's code without comments!
Basics:
- There are 3 main types of loops in pseudocode, Do loops, While loops, and For loops.
- Loops are also known as iteration, meaning the repetition of a block of code.
For Loops in Pseudocode
For Loops (also known as definite or count-controlled iteration) are the perfect way to iterate through a list or an array. They are much easier than they looks, and they are very simple once you start playing with them.
What are For Loops used for?
For loops are used when you know how many times you want to repeat a certain block of code. This is known as definite iteration. This can be very helpful with outputs and inputs in the program!
Using For Loops
Say we wanted to loop through a block of code 5 times, we use i, a local variable, that is built into most programming languages, and can be used in pseudocode too. We would say: For i = 1 To 5; 5 being the number of times you want to loop the code; you can change this to what you would like.
While Loops in Pseudocode
While Loops are very easy to get the hang of! Also known as indefinite iteration, they are used in almost every program!
What are While Loops used for?
We use While loops when we do not know how many times we will run through the code; this can be very helpful when we want to loop some code until a certain event happens. Say we were making a searching algorithm, we would want the loop to continue until we found the thing we were looking for, so we would use a while loop!
Using While Loops
This may look complicated but all we are doing is searching a different index each time. It is really simple. The For loop repeats the while loop, the while loop adds an extra number to i and then it repeats. This will do this until it has found "Battlefield", and then the program will stop.
Do While Loops in Pseudocode
Do While loops are very helpful for iterating whilst waiting for a condition to become true. This is another example of indefinite iteration.
What are Do While Loops used for?
Do While loops are used when it is once again indefinite iteration. This means that while we do not know how many times to loop something, we can just use a Do While loop. This can be very helpful when used with functions and procedures as while the condition is false, the function will run.
Using Do While Loops
This makes pseudocode a lot easier especially with using variables in iteration. If you do not include the Until however, your program will not run and contains errors. So make sure you always include the Until then add a condition after it.
Try our pseudocode editor.
Give our pseudocode editor a go today for free - with a built-in compiler, tools to convert pseudocode to code, and project saving, PseudoEditor makes writing pseudocode easier than ever!
Try it out