About If Statement in C

 About If Statement in C
Techiio-author
Written by Shuvhojit DebFebruary 10, 2022
8 min read
C
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

Let's discuss the if, if-else statements in C.

Introduction

If Statement is simply a set of operations that 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 that needs to be executed
}

If Statement

The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.

if(expression){  
//code to be executed  
} 

blogpost

Different Types of If Statement

These are different types of If Statements. Let us explain in-depth with syntax.

  • If-else statement
  • If-else if-else statement

If-else statement

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-else if-else statement

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.

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
}
}

Output:

if statement instructions

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
}
}

Output:

your favorite place is different city

Now, let us initialize the variable favoritePlaceToVisit with a value say “Vegas” so that else block gets executed.

your favorite place is different city

if-else if-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
}
}

Output:

you have one car
C Language
Programming
If Statement
Else Statement
If else Statement
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance