# Loops and Iteration Last Updated on

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. ### 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

No standard for pseudocode syntax exists. However, there are some commonly followed ways to help make pseudocode written by one programmer easily understood by another programmer, that applies with functions. We have outlined the best ways to write functions in pseudocode with examples.

#### 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!

#### How to use For loops in pseudocode

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.

```For i = 1 To 5
OUTPUT(i) // would output each number 1 To 5.
Endfor```

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:

```For i = 0 To 4 // index always starts at 0: remember that!
OUTPUT(testArray[i]) // would output each element 1 To 5.
Endfor```

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

### While loops in Pseudocode

#### 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!

#### How to use While loops in pseudocode

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 names: “Johnny”, “Daniyaal” and “Matt”. Then we wanted to search through them until we found "Matt", we would create a while loop inside of a for loop. You can see below what that would look like:

```names ← ["Rohan", "Daniyaal", "Matt"]
For i = 0 To 2
While names[i] ≠ "Matt"
i = i + 1
Endwhile
Endfor```

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 “Matt”, and then the program will stop.

### Do loops in Pseudocode

#### What are Do loops used for?

Do 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 loop. This can be very helpful when used with functions and procedures as while the condition is false, the function will run.

#### How to use Do loops in pseudocode

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:

```Do
OUTPUT(fileVariable.readLine) // reads each line of the file until the file ends
Until fileVariable.endOfFile```

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