Anyone Can Code.

## Pseudocode

### The Basics

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:

1. Strings - Strings are usually several characters in length. They can be single words or entire sentences long and they can include numbers as well. It is important to note that strings are always denoted by single or double quotes. For example, ‘FOOD’ is considered a string whereas FOOD is not.

2. Numbers - In computer science, any form of data that contains a numerical value is considered a number. There are separate classifications for decimal numbers and integers but for this tutorial, we will only discuss whole numbers. Keep in note that 2 is a number but ‘2’ is a string, and this applies to every number.

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.

### Variables

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
PRINT NUM

Here the latest value assigned to variable NUM is 6. Therefore the program will output 6.

### Input

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.

• INPUT name1 (name1 is the variable in which the input is stored)
• PRINT name1

### Operators

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 -

1. Arithmetic Operators - These are standard mathematical operators:

Operator Operation
- Subtraction
* Multiplication
/ Division

Example: NUM = 5*5

The variable NUM is now assigned a value of 5 x 5 = 25.

2. Assignment Operators - This operator is used to assign values to variables.

Operator Operation
= 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

For Exammple:

NUM = 4
NUM += 5
PRINT NUM

The result of this code will be 4 + 5 = 9

3. Comparison Operators - These operators are used to compare two variables or values and are used to output Boolean values like TRUE or FALSE. They are very commonly used in conditional statements and loops which will be discussed later.

Operator Operation
== 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

4. Logical Operators - These operators combine two separate expressions into one. They usually deal with the outputs TRUE and FALSE based on the parameters and the original conditions being used. They are mostly used in conditional statements which will be discussed in the next section.

1. AND - The AND operator combines two conditions into one and only gives the output TRUE only if both conditions are true.

Example: 2 == 2 AND 3==4

The output will be FALSE because 2 is equal to 2 but 3 is not equal to 4

2. OR - The OR operator also combines two conditions into one and gives the output TRUE if either of the conditions is true.

Example: 2 == 2 OR 3==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.

3. NOT - The NOT logical operator reverses the condition that is being used.

Example: NOT 2 == 2

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.

### Conditional Statements

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:

IF (*condition*)
*insert code here*
ELSE
*insert code here*

### Nested Conditional Statements

It is possible to put conditional statements within other conditional statements:

• INPUT Number
IF Number < 0 OR Number> 100
PRINT ‘Invalid number’
ELSE
IF Number > 49
PRINT ‘Pass
ELSE
PRINT ‘Fail

### Loops

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:

1. COUNT = *any number*
2. WHILE COUNT < (*any number*) REPEAT STEPS _ TO _
3. *insert code to be repeated here* INCREMENT COUNT BY _

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:

1. COUNT = 0
2. WHILE COUNT < 6 REPEAT STEP 3 TO 4
3. PRINT COUNT
4. INCREMENT COUNT BY 1

The output of the code will be:

1
2
3
4
5
6

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!

Copyright © 2020, Q-Programming | Created by Arjun Sodhi, Balpreet Juneja and Sammarth Kumar | Developed and Designed by Sammarth Kumar