If Statements in Pseudocode
In this tutorial, we will explore the concept of if statements in pseudocode. If statements are a fundamental building block in programming, allowing us to create conditional branches in our code. By the end of this tutorial, you will have a solid understanding of if statements and how to use them effectively in pseudocode.
If statements are a type of control structure that allows us to execute a block of code only if a certain condition is met. They are essential for creating decision-making processes in our programs.
Uses:
- Comparing values.
- Checking if a variable meets a specific condition.
- Determining if a user input is valid.
Basic If Statements in Pseudocode
The basic syntax of an if statement in pseudocode is as follows:
The condition is a logical expression that evaluates to either true or false. If the condition is true, the code within the if statement will be executed. If the condition is false, the code will be skipped.
If Statement - Pseudocode Example
Let's consider a simple example. We want to check if a student's grade is greater than or equal to 60. If it is, we will print 'Pass'; otherwise, we will do nothing.
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.
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:
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:
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:
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!
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.