Updated: Aug 26
When you are first getting introduced to the world of coding it may seem overwhelming. It is essential to understand the basics first, and then you could build on them. One of the core functionalities in programming that is used throughout every object-oriented programming language is the “If” statement.
This article will:
Define what the if statement is
Breakdown the structure of the if statement
Show the different comparisons that can be used in an if statement
Show how it is used in code
What is an if statement?
An if statement is a conditional statement in code that tells your program what to do with certain pieces of information. In English, computers are not smart enough to think fully for themselves, yet. An if statement lets your program make decisions while it is running. You will predefine statements and check that if your program meets the criteria for while it is executing, it will then carry out certain tasks.
If statement showed in diagram form
What is the structure of an if statement?
There are a minimum of 2 parts to start the if <situation> and then <do this action>. From there you can expand to an else and else if statement. If the situation is true in the initial if statement then it will perform an action you specify for it. If the if condition is not met then your code will skip the action portion and continue with the rest of the code.
Along with your initial if statement you can add an else statement that follows it. This is like a safety net. If the previous condition(s) described in the if statement(s) were not met it will default to this block of code. You are only allowed one else statement per block of code and it should go at the end of your if sequence because it catches anything that was not previously handled by the if statement.
The else if statement is a back up to the original if statement. After your initial if statement you can place an else if statement which functions as an extra if statement if the first criteria is not met. Just like in the initial if statement there would be a conditional comparison that checks if a certain condition is true. If it is, it will execute the code in this portion, if it is not it will skip the code in this section. You can have as many else if statements as you like after your initial if statement and before your else statement.
Think of if statements as glorified true or false questions in a conversation. If you ask someone a question, “Is it raining outside”. This is your initial if statement. If they reply yes then you must bring a rain jacket(do this action). If the answer is no you skip this step and proceed to your next question, “Is it snowing outside?” This is your else if statement. If they answer yes to this then you know to bring snow boots. Finally if the answer to both of the previous questions then you will just bring a hat because it must be nice outside because it is not raining or snowing. This your final else statement which shows your default action when the previous criteria are not met.
What are the different comparisons that can be used in an if statement?
Equal to(==) -code executes if object a is equal to object b. For example, if 5 is equal to 5.
Greater than(>)-code executes if object a is greater than object b. For example, if 5 is greater than 4.
Less than(<)-code executes if object a is less than object b. For example, if 5 is less than 6.
Greater than or equal to(>=)-code executes if object a is greater than OR equal to object b. For example, if 5 is greater than 5.
Less than or equal to(<=)-code executes if object a is less than or equal to object b. For example, if 5 is less than 5.
Not equal to(!=)-code executes if object a is not equal to object b. For example, if 5 is not equal to 6.
How is this actually used in code?
If you are new to code anything marked with # is considered a comment and is not run with the code. It is usually used for note-taking. I will place it above each line of code to explain what the line of code is doing. Also please note in the programming language that I am using, Python, the else if syntax is “elif”.
The initial statement will result in a true condition so it will execute the block of code associated with that statement. We then print “I will wear a jacket” as you can see on the last line in white.
The initial condition statement was not a match so we go to the second statement, the elif(else if) statement. It is a match so we execute the block of code associated with it is snowing. We then print “I will wear a jacket” as you can see on the last line in white.
Neither of the initial statements was a match. This will result in the else condition being executed. It will print “ I will wear a hat” as you can see on the last line in white.