# If - else

If-Else statements are one of the most fundamental decision control statements used in C Programming. These statements allow you to control the flow of the program based on a certain condition. In this article, we will explore the basics of If-Else statements, their syntax, examples, and related concepts. Syntax of If-Else Statements: The basic syntax of If-Else statements in C Programming is as follows:

copy

``````if (condition) {
statement1;
}
else {
statement2;
}``````

The "if" block is executed if the condition is true. Otherwise, the program will execute the "else" block. Example: Let's take an example to illustrate If-Else statements in C Programming.

copy

``````#include <stdio.h>

int main()
{
int num;
printf("Enter a number \n");
scanf("%d", &num);
if (10 < num)
{
printf("Your number is greater than 10");
}
else {
printf("Your number is less than or equal to 10");
}
return 0;
}``````

In the above example, if the entered number is greater than 10, then the output will be "Your number is greater than 10". Otherwise, the output will be "Your number is less than or equal to 10".

## Else-If Statements:

The Else-If statement is an optional part of the If-Else statement. It allows you to check between multiple test expressions and execute different statements. The basic syntax of the Else-If statement is as follows:

copy

``````if (condition1) {
statement1;
}
else if (condition2) {
statement2;
}
else if (condition3) {
statement3;
}
...
else {
statementN;
}``````

In the above syntax, the program will check the conditions from top to bottom. If the first condition is true, it will execute "statement1". Otherwise, it will check the second condition, and so on until it finds a true condition. If all the conditions are false, then the program will execute "statementN". Example: Let's take an example to illustrate the Else-If statement in C Programming.

copy

``````#include <stdio.h>

int main()
{
int num;
printf("Enter a number \n");
scanf("%d", &num);
if (10 > num)
{
printf("Your number is less than 10");
}
else if (25 > num)
{
printf("Your number is less than 25");
}
else if (50 > num)
{
printf("Your number is less than 50");
}
else {
printf("Your number is greater than or equal to 50");
}
return 0;
}``````

In the above example, if the entered number is less than 10, then the output will be "Your number is less than 10". If the entered number is greater than or equal to 10 and less than 25, then the output will be "Your number is less than 25". If the entered number is greater than or equal to 25 and less than 50, then the output will be "Your number is less than 50". Otherwise, the output will be "Your number is greater than or equal to 50".

## Nested if

Nested if statements are a powerful tool in programming that allows you to create more complex decision-making structures in your code. These statements involve using multiple if-else blocks that are nested within each other, allowing you to check multiple conditions and take different actions based on the outcome. Here's an example of how a nested if statement can be used to determine if someone is eligible for a driver's license:

copy

``````#include <stdio.h>

int main()
{
int age, lic;
scanf("%d", &age);

if (age >= 18)
{
printf("You are above 18.\nDo you want to apply for a license?\n");
printf("If yes, enter 1. Otherwise, enter any number.\n");
scanf("%d", &lic);

if (lic == 1)
{
}
else
{
}
}
else
{
printf("Sorry, you are not eligible for a driver's license.\n");
}
return 0;
}
``````

In this code, the user is prompted to enter their age. If they are 18 or older, the program checks whether they want to apply for a license by asking them to enter either 1 or any other number. If they enter 1, the program prints a message saying their license has been applied for. If they enter any other number, the program prints a message saying their license has been canceled. If the user is younger than 18, the program prints a message saying they are not eligible for a driver's license. As you can see, nested if statements can help you create more complex decision-making structures in your code. They allow you to check multiple conditions and take different actions based on the outcome, which can be very useful in a variety of programming tasks.