Discussion of the if else statement in C, as well as its flowchart and examples

Discussion of the if else statement in C, as well as its flowchart and examples
Techiio-author
Written by Shuvhojit DebFebruary 10, 2022
6 min read
C
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

The purpose of this blog post is to teach you how to write if else statements in C and to provide examples of them.

About if-else statement in C:

The if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simultaneously. Using an if-else statement is always preferable since it always invokes an otherwise case with every if condition.

Syntax:-

if(expression){  
//code to be executed if condition is true  
}else{  
//code to be executed if condition is false  
}  
Flowchart of if else statement:
blogpost
Examples of if else statement:

Let’s take an example of a Boolean expression with the help of actual coding in C: If the condition is met (true) as per the given logical expression, then the program will print the statements under ‘if’ and if the condition is not met (false) then the program will print the statements which are under ‘else’ and below the ‘if-else.’

Example No:1

To print a simple ‘if’ condition:

Code:-

#include <stdio.h>
int main( )
{
int a, b;
a = 15;
b = 10;
if (a > b )
{
printf("a is greater than b");
}
}

Output:-

a is greater than b

Example No:2

To check whether a number is greater or less than a specific number:

Code:-

#include <stdio.h>
int main ()
{
int a = 10; /* local variable definition */
if( a > 5 ) /* check the boolean condition */
{
printf("Yes, a is greater than 5" ); /* if the condition is true then print this */
}
else
{
printf("No, a is less than 5" ); /* if the condition is false then print this */
}
printf("The value of a is: %d", a);
return 0;
}

Output:-

Yes, a is greater than 5
The value of a is: 10

Example No:3

To check which one is the greater among the 3 numbers:

Code:-

#include <stdio.h>
int main( )
{
int a, b, c;
printf("Please enter three numbers: ");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c){printf("a is the greatest among the three"); }
else{printf("c is the greatest among the three");}
}
else
{
if(b > c){printf("b is the greatest among the three");}
else{printf("c is the greatest among the three");}
}
}

Example No:4

To check if an input number is divisible by 2 or 3:

Code:-

#include <stdio.h>
void main( )
{
int a;
printf("Please enter a number: ");
scanf("%d", &a);
if(a%2 == 0 && a%3 == 0)
{
printf("The entered number is divisible by both 2 and 3");
}
else if(a%2 == 0)
{
printf("The entered number is divisible by 2");
}
else if(a%3 == 0)
{
printf("The entered number is divisible by 3");
}
else
{
printf("The entered number is divisible by neither 2 nor 3");
}
}
Conclusion:

In this composition, we've bandied if-additional tentative statements and how it works with a flow diagram and a Program, grounded on a given condition for C programming language, which is analogous to any other programming language. If we ignore the‘ additional’ part of the program statement also we can simply show the result of‘if’ condition as well without considering the additional part that too is allowed.

C Language
Programming
If else statement
if else
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 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