Conditions, if statements in this case, is where programming starts getting interesting.

Conditions is what you use to give your application logic. You can now take some variable or input and based on that, do some specific instruction.

The structure of an if statement tends to follow the structure below.

It starts by using the “if” keyword, followed by the variable / input, then followed by the comparator (These will be listed below and explained). Then finally the bit of code you want to run if all values are true, you can then use a second function of an “if” statement called an “else“. Which is if the check returns false, then do something else.

A plain English example would like like the below.

if oranges are orange then put them in a bag else leave them behind.

In our “Pseudo Code” we will start as normal with the if keyword, then put the check in brackets, to draw attention to it and we will use the “then” and “else” keywords to break up the code. An example below:

//age is set to 18
number Age = 18

//This if statement checks if you are over 20
If (age > 20) then
    display message "You are older than 20"
    display message "You are 20 or younger"
//The output here will be You are 20 or younger

Below you can see a visual example of an if statement/conditional.

Now that you know how to structure an if statement, let’s looks at the different operators.

Equal==Checks if 2 values are equal. Because we use 1 equals to assign a value, we will use 2 equals compare1==1
This is true
Not Equal!=This checks to see that 2 values are not Equal. 1!=1
This is false
Greater than>Checks if a value is greater than another3>1
This is true
Less than<Checks if a value is smaller than another1<3
This is true
Greater than or equal to>=Checks if a value is greater than another or if it’s the same 3>=2
This is true
Less than or equal to<=Checks if a value is less than or equal to another3<=4
This is true

You now know the basics of operators the operators, let’s see them used in if statements to see all the operators in all states.

(1==1) this is true
(1==2) this is false

//Not Equal
(1!=2) this is true
(1!=1) this is false

//Greater Than
(1>0) this is true
(1>2) this is false
(1>1) this is false

//Less Than
(1<2) this is true
(1<0) this is false
(1<1) this is false
//Greater Than Or Equal (1>=0) this is true (1>=1) this is true (1>=2) this is false //Less Than Or Equal (1<=2) this is true (1<=1) this is true (1<=0) this is false

Congratulations! You have leveled up in programming!

Next – Demystifying Programming – 5 Loops

Previous – Demystifying Programming – 3 Arithmetic

Leave a Reply

Your email address will not be published. Required fields are marked *