In this section, we will learn what the if-else statements are and how to use them in C.
C if Condition Statement (AKA if-then statement)
We use `if` statement in pretty much every day of our lives.
- We’ll stay at home if it rains.
- If you need money, I can lend you some.
- If I didn’t apologize, I’d feel guilty.
- If you had worked harder, you would have passed your exams.
It is used to check whether a condition is true, which in that case only a work can be done.
In the world of programming, we have the same concept as well.
You see, our program can react based on different conditions. For example, if we’re building an e-commerce website, we can use the `if` statement to see if the total value in a user’s shopping cart is more than $90 but less than $100 for example, in that case send a message to her and remind her if she orders only $10 more, she’ll get a 10% discount on her purchase.
This was just one of many use cases of the if statement. Basically, the `if-statement` is one of the most used-statement in not just C but many other programming languages.
C if-else syntax:
This is the syntax of the if statement in C Programming:
if (condition){ //body of the if-statement where code is and will be run if the condition is true }else{ //the code here will only run if the condition is not true. }
`if`: this is the keyword that we use for starting an `if-statement`.
`()`: followed by the `if` keyword is a pair of parentheses which contains a boolean expression. The result of the expression that we define within these parenthesis, is the deciding factor on whether the body of the `if` statement can be executed. Basically, if the result of the expression in these parentheses was true, that means the instructions within the body of the target if statement can be executed. Otherwise, if the result of the expression was false, that means the CPU must skip over the body of the target if statement.
Note: an example of boolean expressions that could be used within this pair of parentheses would be the use of comparison operators. For example, 4 < 5 is a comparison operation that uses the Less-Than operator.
`{}`: within the body of brackets that come after `()` we declare our instructions to be run if the condition is true. Basically, the pair of braces after the `()` defines the body of the if statement and the execution of any instruction within this body, depends on the result of the condition of that if statement. (If the condition resulted true, the instructions of the body will be executed, otherwise they’ll be skipped).
`else`: if it turns out that the defined condition of the if statement is not true, then the instructions within the body of `else` statement will run. The body of the `else` statement is within the brackets` {} ` that come after the `else` keyword. You can think about the `else` statement as the plan B if you will. Basically, the CPU first focuses on the if statement and only if the condition of this statement resulted false, in that case only, it will move on to the body of the related else statement to run its instructions.
Example: using C if-else statement
#include <stdio.h> int main() { int age = 13; //Is the person allowed to go to a Bar? if (age < 21){ //This line will be run if the condition is true. printf("No, this person can't go to a Bar. "); }else{ // In case the condition was not true, the instruction in this body will be run. printf("Yes, this person can go to a Bar. "); } return 0; }
Result:
No, this person can't go to a Bar.
How Does if-else statement work?
In this example, we’ve used the `if` condition to see if the value in the `age` variable is less than 21 via the less-than-operator `<`.
The value in the `age` variable is not greater than 21 and so the condition within the `if` statement is true. So the instructions in its body will run.
Note: depending on the result of the condition, either the `if` statement or `else` statement will run, but not both!
C if-else-if (AKA ladder) statement
Sometimes there are multiple conditions and we need to run a specific set of instructions based on each of these conditions. This is where the if-else-if statement comes in.
C if-else-if syntax
if (condition_1){ //body of the if-statement where code is and will be run if the condition is true }else if (condition_2){ //body of the else-if-statement where the code is and will be run if the condition_2 is true }else if (condition_N){ //body of the else-if-statement where the code is and will be run if the condition_N is true }else{ //the code here will only run if none of the conditions in `if` and `else if` result true. }
In the structure above, there’s a new keyword which `else if`.
`else if`: the `else if` is used to define a new condition if the first condition resulted false.
Notes:
- You can use as many `else if` statement as you want but the first condition in the `if` statement always starts with the `if` keyword alone and after that, if we want, we can add one or more `else if`.
- The use of `else if` is optional and only used to specify more conditions.
- The order of execution of conditions is linear from top to bottom. If any of the conditions resulted true, the rest of `else if` and `else` statements will be skipped.
- The `else` statement always comes as the last statement and will run only if none of the conditional blocks before the else statement resulted true. In another word, if all the conditions were false, in that case the body of the `else` statement will run.
- The use of `else` statement is optional.
- We can only use ONE `else` statement along with an `if` statement.
Example: C if-else-if statement
#include <stdio.h> int main() { int value = 200; if (value == 100){ printf("The value is: 100"); }else if(value == 200){ printf("The value is: 200"); }else if(value == 150){ printf("The value is: 150"); }else{ printf("None of the conditions matched the value\n"); } return 0; }
Output:
The value is: 200
How Does C if-else-if Statement Work?
In the example above, the condition of the first `if` statement did not match (results false) because the number assigned to the `value` variable is not equal to 100.
When the second condition, which is for the `else if` statement executed, it resulted true and so the body of this statement is the one that runs.
Note: the rest of `else if` and `else` statements will be skipped.
C Nested if..else Statement
The body of an if statement is open for any sort of operation! This includes using another if statement within the body of an if statement which is known as nested if statement.
Example: creating nested if…else statement in C
Let’s see a dummy example just to give a hint of how this works:
#include <stdio.h> int main() { if (10>4){ if (15>10){ printf("%s","The value 15 is bigger than the value 10"); }else{ printf("%s","The value 15 is less than the value 10"); } } return 0; }
Output:
The value 15 is bigger than the value 10