What is If Statement in C?
If Statement is simply a set of operation which could be used to compare expressions. These generally have two values of LHS and RHS. This operator compares the expression of the left-hand side and right-hand side. In comparison, it simply returns a Boolean value.
Syntax
The general syntax of If Statement in C is,
if(expression to be evaluated ) {
// sets of instruction which needs to be executed
}
Explanation of Syntax
Now, let us understand the above syntax
A general If Statement compromises in an above-mentioned manner, and it contains different parts. Let us explain each part.
- Expression to be evaluated – In this part, evaluation of the statement is done. This section generally comprises of the left-hand side and right-hand side. Both left-hand sides, which are LHS and a right-hand side, RHS, are compared and evaluated. If the LHS is equal to RHS or the expression is true, then the control enters in the if-section
- Sets of instruction which needs to be executed – If the expression of the if block is satisfied, then the sets of instruction which needs to be executed are executed
A typical example of the first part could be if “1 is less than 10”, and a simple example of code which needs to be executed could be to print any number.
Different Types of If Statement in C
These are different types of If Statement. Let us explain in-depth with syntax.
- If-else statement
- If-elseif-else statement
If-else statement
In this syntax is similar to:
if(expression to be evaluated ) {
// sets of instruction which needs to be executed
} else {
// sets of instruction which needs to be executed
}
If-elseif-else statement
In this syntax is similar to:
if( expression to be evaluated ) {
// sets of instruction which needs to be executed for if-block
} else if{
// sets of instruction which needs to be executed for else-if block
} else {
// sets of instruction which needs to be executed for else block
}
In this section, each block is evaluated, and the code is executed as per the evaluation.
Now let us see the general flow-chart of the If Statement in C
Flow diagram of If Statement
Below given represents a flow diagram of the If Statement.
Examples
Now, let us understand the above-mentioned syntax with examples
If Statement example
Let us look at this with an example
#include <stdio.h>
void main () {
int varNumValue = 1;
if( varNumValue < 10 ) { // checks the condition
printf("if statement instructions"); // sets of instructions which needs to be executed
}
}
Now, copy the above code snippet and run it
It will show the following output
If-else statement example
Let us look at this with an example
#include <stdio.h>
void main () {
char favoritePlaceToVisit[] = "New York";
if (favoritePlaceToVisit == "New York") { // checks the condition
printf(" Your favorite place to visit is New York "); // sets of instructions which needs to be executed for if block
} else {
printf("Your favorite place is different city"); // sets of instructions which needs to be executed for else block
}
}
Now, copy the above code snippet and run it
It will show the following output:
Now, let us initialize the variable favoritePlaceToVisit with a value say “Vegas” so that else block gets executed.
Let us look at this with an example
#include <stdio.h>
void main () {
char favoriteFruit[] = "Apple";
if (favoriteFruit == "Kiwi") { // checks the condition
printf("You like to eat Apple"); // sets of instructions which needs to be executed for if block
} else {
printf("You don't like to eat Apple"); // sets of instructions which needs to be executed for else block
}
}
Now, copy the above code snippet and run it
It will show the following output:
Now, it is easy to understand what is If Statement and what is an if-else statement.
Example of if-elseif-else statement
#include <stdio.h>
void main () {
int enterNumberOfCarsYouHave = 1;
if( enterNumberOfCarsYouHave == 1 ) { // checks the condition
printf("You have one car"); // sets of instructions which needs to be executed for if block
}
else if( enterNumberOfCarsYouHave == 2 ) { // checks the condition
printf("You have two cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 3 ) { // checks the condition
printf("You have three cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 4 ) { // checks the condition
printf("You have four cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 5 ) { // checks the condition
printf("You have five cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 6 ) { // checks the condition
printf("You have six cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 7 ) { // checks the condition
printf("You have seven cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 8 ) { // checks the condition
printf("You have eight cars"); // sets of instructions which needs to be executed for if else block
}
else if( enterNumberOfCarsYouHave == 9 ) { // checks the condition
printf("You have nine cars"); // sets of instructions which needs to be executed for if else block
}
else {
printf("You have more than 10 cars"); // sets of instructions which needs to be executed for else block
}
}
Now, copy the above code snippet and run it
It will show the following output:
Conclusion
C is a programming language where there are lots of concepts that one needs to study. Suppose the statement is one of those. These operators basically execute the code to check whether the expression value is true or not. Based on the expression evaluation, it executes the code. And if the statement is widely used in any programming language to various logical programming expressions.
Recommended Articles
This is a guide to If Statement in C. Here we discuss the different types of If Statement with the appropriate explanation of the Syntax along with sample code. You may also have a look at the following articles to learn more –