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!

Code Icon

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.


Loading...

We can also then use the i variable inside of our code. So if we were sorting through an array, we could use i as the index. So we would say: testArray[i]. So, when the program loops through the code, it will use the element that is at the point of i in the array. Say the array was 5 elements long, our For loop would be as follows:


Loading...

This is because the variable i increments each pass, so we get a different element each time.


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 For 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

Let's say we were making a searching algorithm. We would want to search the array until we found the element we were looking for. Say we had an array of video games: Fortnite, Call of Duty and Battlefield. Then we wanted to search through them until we found "Battlefield", we would create a while loop inside of a for loop. You can see below what that would look like:


Loading...

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

For this example we will be reading a text file in pseudocode. This is where Do loops become particularly useful; we can read each line of the file until we reach the end of it. This means that we do not have to use any annoying iteration techniques but rather just use a Do loop. Do loops are always followed by Until. So for reading a file it would be:


Loading...

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.


Code Icon

I hope this helped you, you should now understand what iteration is, how to use For, While, and Do loops in pseudocode and even what loops are used for in pseudocode.


Read Our Guide on
Variables
in Pseudocode



© 2023 PseudoEditor.com | All rights reserved

Useful Links

Sign InSign UpFeaturesFAQ