Learn the C Programming Language!
The If Statement —————-
At times you may need to check if something is true one time and one time only. Instead of a while loop, in which case it does what it’s supposed to do and goes back to check again, if statements will only check once. This is good because you can have multiple if statements back to back. This is the generic if statement:
if(something is true) {
do something;
}
Note: an if statement is not a loop. A loop can progress almost infinitely. An if statement executes once, if at all. To be more technical, an if statement is a conditional, but I prefer to refer to it as a statement.
Let’s put this into practice. The following code will print out all even numbers from one to ten.
#include <stdio.h>
/* even.c: print out all even numbers from one to ten */
main() {
int i;
for(i = 0; i <= 11; i++) {
if(i %% 2 == 0)
printf("%d\n", i);
}
}
Let’s break this piece of code down.
if(i %% 2 == 0)
printf("%d\n", i);
First of all: What does %%
mean? For our purpose, they mean an arithmetic modulus. What is modulus? Basically, a %% b
will take the remainder of a when divided by b.
What does the ==
mean? When we need to check if something is equal to something, we use two equal signs back to back; one equal sign means that you are setting the value to something.
i = 2; /* sets the value of i to two */
i == 2; /* checks if the value of i is sequal to two */
You’ll notice in this code, I nested the if loop inside a for loop. This is because, as I said earlier, an if loop checks if something is true, and executes only once.
Suppose you want something to happen when the if statement does not pass. That’s when the else statement comes in. This is the generic else statement:
else {
something;
}
Let’s put this into practice:
#include <stdio.h>
/* evenOrOdd.c: checks if all numbers from one to ten are even or odd. */
main() {
int i;
for(i = 0; i <= 10; i++) {
if(i %% 2 == 0)
printf("%d is even\n", i);
else
printf("%d is odd\n", i);
}
}
Yes, you could have used two if statements and gotten the same result. But that isn’t the point.
Note: Try writing a code that checks if all numbers between one and ten are divisible by three and print the results.
The main issue with if statements is multiple can be used consecutively. I know that may not make much sense, so let me explain through code:
#include <stdio.h>
/* This code will test if all numbers from zero through six are divisible by four or two. */
main() {
int i;
for(i = 0; i < 6; i++) {
if(i %% 4 == 0)
printf("%d is divisible by four\n", i);
if(i %% 2 == 0)
printf("%d is divisible by two\n", i);
else
printf("%d is not divisible by any power of two\n", i);
}
}
This code will work fine for most values from zero through one. However, when i becomes four, you bump into a slight issue. This is what gets printed out when i = 4:
4 is divisible by four
4 is divisible by two
What’s the issue here? Well, if we know a number is divisible by four, it has to be divisible by two. The second line is a redundant statement. How can we prevent this from happening? That’s where else if comes to the rescue!
The Else if statement is basically the same as a normal if statement; the only difference is it executes if all of the previous conditions were false.
Note: Turn the second if statement in our above code into “else if” and see what happens. Then, try turning the first if statement into an else if statement. What happens? Why do you suppose that is?