Introduction to If-else Statement in C
If else Statement in C programming language, when we need to execute a block of statements that too when a particular condition is met or not met that situation is known as decision making. In C programming, the decision-making process is used to specify certain orders in which statements are executed. In this topic, we are going to learn how the if-else condition is used and when with some diagrams & codes. In C programming language, the ‘if’ statement can be implemented in four basic forms depending upon usage in different variants or the complexity of the logical condition or requirements.
The ‘if’ conditional statements are as below:
- if statement
- if-else statement
- Nested if-else statement
- else-if statement
Syntaxes
Below are some syntax of if statement in c is as follows
1. The syntax for if statement
if(condition or statement)
{
/* statement inside the if body */
}
/* statement outside the if body */
2. The syntax for if-else statement
if (condition)
{
/* Statements inside the body of ‘if’ logical condition */
}
else
{
/* Statements inside the body of ‘else’ logical condition */
}
3. The syntax for nested if-else statement
if( condition or statement )
{
if( expression1 )
{
/* statement1 */
}
else
{
/* statement2 */
}
}
else
{
/* statement3*/
}
4. Syntax for else-if statement
if(condition1)
{
/* statement1 */
}
else if(condition2)
{
/* statement2 */
}
else if(condition3 )
{
/* statement3 */
}
else
/* default statement */
Flow diagram
For example:
How if-else Statement Works in C?
Basically, if the condition returns to be true then the statements mentioned inside the body of the logical ‘if’ are met or executed and the statements inside the body of ‘else’ are skipped. Similar way, if the condition returns as false in the logic then the statements inside the body of ‘if’ are skipped and the statements inside the ‘else’ are executed. To understand the concept better, let’s take an example of “xyz expression”:
If the “xyz expression” is considered to be true then,
- The statement(s) under the ‘if’ condition is returned.
- The statement(s) under the ‘else’ condition is ignored from execution.
If the “xyz expression” is considered to be false then,
- The statement(s) under the ‘else’ condition is returned.
- The statement(s) under the ‘if’ condition is ignored from execution.
For example:
Examples
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 #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 #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 #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 #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 article, we have discussed if-else conditional statements and how it works with a Flow Diagram and a Program, based on a given condition for C programming language, which is similar to any other programming language. If we ignore the ‘else’ part of the program statement then we can simply show the result of ‘if’ condition as well without considering the else part that too is allowed.
Recommended Articles
This has been a guide to If-else Statement in C. Here we discuss an introduction to If-else Statement in C, how does it work with proper coding and output along with examples and a flow diagram. You can also go through our other suggested articles to learn more –