Programming Basics: The If Statement
Why do we need the if statement?
Since our algorithm will take on different states throughout its execution, and we will have variables, we need a way to check whether our algorithm is in a given state or if a variable contains a specific value. To have conditional code execution, that is, to be able to say: if A is true then do action B, we need to use the if statement. The syntax for an if statement is as follows:
if(condition) {
//some code
}This translates to if the “condition” evaluates to true then execute “some code”
Now you need to understand what evaluates to true means in this context. This is one of the most important points that many beginners don’t really know about or think about when learning the if statement. If statement only understands two types of values: true or false
That’s it. It doesn’t understand what your variable x is or if it is equal to 10 or whatever. Let’s look at an example:
int x = 10;
if(x == 10) {
printf("x is equal to 10");
}Note: Disregard the printf statement (actually a function) for now. All you need to know is that once we do printf(“something”), “something” will be displayed on the screen.
Note: The word “Boolean” refers to a data type of a variable that may only take one of two values: {true, false}
This code will output:
x is equal to 10
Because the expression we have passed as condition to the if statement is:
x == 10
Let’s break it into three parts
We have an operand, an operator and another operand. The == operator acts like a function that takes two parameters and returns a boolean. That sounds confusing, since you don’t know what functions are. Basically, if the two operands are equal, this expression will yield true, otherwise false.
Get Muhamed Cicak’s stories in your inbox
Join Medium for free to get updates from this writer.
Which means, the if statement at the very end will get either true or false as its condition, which it can directly understand. If it’s true, it will execute the code between the curly braces, otherwise it won’t.
The reason why you need to understand the “evaluates to true” part is because we can write if statements like this:
if(15) {
// some code
}Now this would be confusing if you didn’t know that 15, or any number different from 0, when used in context of evaluating to a boolean value, evaluates to true. So above, our “some code” will be executed. Likewise, if we write:
if(0) {
//some other code
}In this case, “some other code” would not execute, as 0 will evaluate to false. We say that the value 0 is falsy in C. Note that what evaluates to true and false is determined by the language you are using. That is, the language determines what values are falsy and what values are truthy. For example, in a programming language called Ruby, 0 is truthy. So in Ruby,
if 0
# This block will be executed, as 0 is truthy in Ruby
endNow, let’s implement a simple algorithm. Let’s create a program that takes two integers, if their sum is over 15, it multiplies their sum by 2 and prints it, otherwise it just prints their sum. This time I’ll write the whole program, not just the algorithm part. Here goes:
#include <stdio.h>
int main() {
int x = 7; // first number
int y = 9; // second number
if(x + y > 15) {
printf("%d", 2 * (x + y));
} else {
printf("%d", x + y);
}
}This code will output (that is, print) 32, because x + y which is 7 + 9 will evaluate to 16, and in the if condition we will get 16 > 15, which will evaluate to true, and the if statement’s body will be executed, printing 2 * (x + y) which is 2 * 16 which is 32. The else statement will be skipped and not executed, since the if statement evaluated to true.
Note: This course does not focus on explaining syntax rules in detail, so if you get confused with any of the C code that’s written in the blog series, please check out other sources to specifically learn more about the syntax used in C.
Now here we can see something new: the else statement. The else statement means the “otherwise” part we use when talking. So, if the sum of the two numbers is greater than 15 then do “something”, else (otherwise) do something else. So if the condition in the if statement is not true then the else statement’s body will execute.
And let’s immediately point out that this code can be rewritten to be better. What do I mean by “better”? It is more useful because it is easier to read, modify and scale. We can improve it by creating a variable for the value of x + y. Because the expression x + y is repeated three times but what we really want is for those three occurrences of the expression to be codependent. Right now they are independent. Each expression is evaluated again and if we change one of the occurrences the others won’t change, which is not desirable in this context. So, we rewrite the code as following:
#include <stdio.h>
int main() {
int x = 7; // first number
int y = 9; // second number
int sum = x + y; // the new variable
if(sum > 15) {
printf("%d", 2 * sum);
} else {
printf("%d", sum);
}
}This code behaves exactly the same way as the older version, it will output 32.
Please note that it is not required or even expected for complete beginners to completely understand this part, you can always come back and reiterate it for better comprehension.
That’s enough for the if statement for now. We will constantly encounter the if statement throughout our journey and better understand as we dive deeper.








