Anyone Can Code.
In computer science, pseudocode is just a clear and concise way to represent an algorithm. Looking at the name itself, ‘pseudocode’ literally translates to “non-genuine code”. However, pseudocode is very helpful in understanding the logic and key instructions that are widely used in programming languages, and it is a fairly easy concept to grasp.
Let’s start off with a very important question -how do we get to see the result of our code on a screen? There is a very simple answer to it - PRINT. In pseudocode, this ‘PRINT’ statement is used to output anything we want to the screen. Other programming languages use different variations of this statement but for simplicity’s sake, we will use PRINT. A very common display of this statement -
Code: PRINT “Hello World”
Output: Hello World
This probably gets you thinking , “if we can print sentences or words, can we print numbers too?” The answer is yes. However, it is very important to define what is considered a word and what is considered a number. For such classifications, we have data types in computer science. They are:
As a rule of thumb, anything inside single or double quotes will be considered as a string by the system.
Now, we can combine PRINT statements with our knowledge of data types.
Just printing strings and numbers by themselves isn’t very helpful. Usually algorithms deal with manipulation of values and sentences. In order to store a certain value so that it can be used for later, we use something called variables. Variables are used to store some information (this includes numbers and strings) and we can also change the values that variables can store. We can declare a variable in the following manner:
NUM = 8
Here, our variable is NUM and the value assigned to it is 8
We can also assign sting values to variables:
WORDS = "This is a string"
Here, our variable is WORDS and the value assigned to it is "This is a string"
Variables are named similar to the way objects are named in real life - any combination of letters and numbers can be used as a variable. For example, NUM1 and Forever can be used as variable names. It is also to note that variable names are case-sensitive so NAME and name will be considered different variables. We can also use the same variable multiple times within a program, however the value the variable holds is the latest one. For example, if we have this code:
NUM = 8
NUM = 4
NUM = 6
Here the latest value assigned to variable NUM is 6. Therefore the program will output 6.
Programs tend to make use of something called inputs. Inputs are given to a program by the user (of the same data types discussed earlier) and the program usually stores this input value in a variable. Once the input is stored, the program can utilize it for multiple other operations.
Operators are symbols that are used within a programming language to perform certain instructions or operations on variables and inputs. There are four types of operators -
Example: NUM = 5*5
The variable NUM is now assigned a value of 5 x 5 = 25.
|=||Asigns the variable a value equal to the value on the right|
|+=||Asigns the variable a value equal to the value on the right plus the existing value of the variable|
|-=||Asigns the variable a value equal to the existing value of the variable minus the value on the right|
|*=||Asigns the variable a value equal to the value on the right times the existing value of the variable|
|=||Asigns the variable a value equal to the existing value of the variable divided by the value on the right|
The result of this code will be 4 + 5 = 9
|==||Checks if value on left is equal to value on right|
|!=||Checks if value on left is not equal to value on right|
|>||Checks if value on left is greater than the value on right|
|<||Checks if value on left is less than the value on right|
|>=||Checks if value on left is greater than or equal to the value on right|
|<=||Checks if value on left is less than or equal to the value on right|
The output will be FALSE because 2 is equal to 2 but 3 is not equal to 4
The output will be TRUE because 2 is equal to 2 even though 3 is not equal to 4. This is because the OR operator checks to see whether at least one of the conditions holds true.
The output will be FALSE because if we want to know whether 2 is not equal to 2, the answer is false, as 2 is equal to 2
Logical operators do not make as much sense on their own as they do when used in conditional statements, which we will cover as the next topic.
A lot of the operators mentioned above won’t be that useful without the existence of conditional statements. These statements, as the name suggests, are used to check conditions. These conditions may include comparing the value of a variable to a given value or checking whether conditions are true or false.
Conditonals are come in a pair of IF and ELSE statements. The IF statement contains the code the program should run if the condition is satisfied, while the ELSE statement contains the code in case the condition is not satisfied. The syntax (format) for using conditional statements is as follows:
It is possible to put conditional statements within other conditional statements:
To make life easier and more efficient, loops are used in programming in order to repeat a certain instruction or command. If loops hadn’t existed, we would be spending hours and hours writing the same few lines of code again and again.
The loop structure is as follows:
Here, the code inside our loop will keep repeating until the COUNT variable is less than a given value. So, if we set COUNT as 0 initially, keep the condition as COUNT < 5, and increment COUNT by 1, the code inside our loop will run 5 times as COUNT goes from 1 to 2 to 3 to 4, but when it becomes 5, the loop ends as 5 is equal to 5.
Let's look at an example for more clarity:
The output of the code will be:
That's the end of the tutorial! if you enjoyed it please check out our other tutorials and articles, all for FREE. Also check out our YouTube Channel and Instagram. We put daily content including Word of the Day, Fun Facts, Quote of the Week, Quizzes and much more!