- A statement that has a condition is called a conditional statement. When we want to make a decision we use conditional statement in C.
- They are also called decision-making statements or selection statements.
- The conditional statements in C language are if statements and switch statements.

Table of Contents
If statement:
- It is a conditional statement.
- The different forms of the if statement are
- Simple if
- If else
- Nested if
- Else if ladder
Simple if statement:
- The syntax of a simple if statement is
if(expression)
{
Statement 1;
Statement 2;
….
Statement n;
}
- The expression in the ‘if’ statement should be either a logical expression or a relational expression.
- When we evaluate this expression it results either true or false.
- If the expression evaluates true value the statements which are written under the expression are executed.
- If the expression evaluates a false value just it skids the if statement and executes other statements in the program.
- If we have only one statement under if condition there is no need to specify open brace and close brace. But we have multiple statements under the condition the statements have to be enclosed within the flower braces.
Write a program to find the largest of two numbers using a simple if statement.
#include <stdio.h> int main() { int num1, num2; // 1. Get input from the user printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); // 2. Compare the two numbers if (num1 > num2) { // If num1 is greater, print num1 as the largest printf("%d is the largest number.\n", num1); } else if (num2 > num1) { // If num2 is greater, print num2 as the largest printf("%d is the largest number.\n", num2); } else { // If both numbers are equal, print that they are equal printf("Both numbers are equal.\n"); } return 0; // Indicate successful program execution }
Output:
Enter the first number: 15
Enter the second number: 6
15 is the largest number.
If else statement:
- Another kind of if statement is the if else statement. It is a two-way selection statement.
- The syntax of this statement
if (expression)
{
Statement 1;
Statement 2;
}
else (expression)
{
Statement 1;
Statement 2;
}
- In this kind of statement first, it evaluates the expression.
- If the expression evaluates true value the statements written under the if statements are executed.
- If the expression evaluates the false value it comes to the else part and executes the statements under the else part.
- If we have only one statement there is no need to put open brace and close brace.
- But if we have multiple statements then we have to put open brace and closed brace.
Write a Program to find the smallest of two numbers using an if-else statement in c.
#include <stdio.h> int main() { int num1, num2; // 1. Get input from the user printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); // 2. Compare the two numbers to find the smaller one if (num1 < num2) { // If num1 is smaller, print num1 as the smallest printf("%d is the smallest number.\n", num1); } else if (num2 < num1) { // If num2 is smaller, print num2 as the smallest printf("%d is the smallest number.\n", num2); } else { // If both numbers are equal, print that they are equal printf("Both numbers are equal.\n"); } return 0; // Indicate successful program execution }
Output:
Enter the first number: 14
Enter the second number: 24
14 is the smallest number.
Nested if statement:
- An if statement that is included in another if statement is called a nested if statement. Generally, we use nested if statements if we want to specify more than one condition.
- Syntax:
If (expression1)
{
{
If(expression2)
}
Statement 1;
Statement 2;
}
Else
{
Statement 3;
Statement 4;
}
}
Write a Program to find the largest of three numbers using a nested if statement in C.
#include <stdio.h> int main() { int num1, num2, num3; // 1. Get input from the user printf("Enter the first number: "); scanf("%d", &num1); printf("Enter the second number: "); scanf("%d", &num2); printf("Enter the third number: "); scanf("%d", &num3); // 2. Compare the first two numbers if (num1 >= num2) { // If num1 is greater than or equal to num2, check it against num3 if (num1 >= num3) { // If num1 is also greater than or equal to num3, it's the largest printf("%d is the largest number.\n", num1); } else { // Otherwise, num3 is the largest printf("%d is the largest number.\n", num3); } } else { // If num2 is greater than num1, check it against num3 if (num2 >= num3) { // If num2 is also greater than or equal to num3, it's the largest printf("%d is the largest number.\n", num2); } else { // Otherwise, num3 is the largest printf("%d is the largest number.\n", num3); } } return 0; }
Output:
Enter the first number: 24
Enter the second number: 68
Enter the third number: 55
68 is the largest number.
Else if Ladder:
- The other alternative way for nested if is else if ladder. A multi-path decision is a chain of if’s in which the if statement is associated with another.
- The conditions are evaluated from top to bottom. Whenever the expression evaluates true value the statements under that expression are executed.
- If all the expressions evaluate false value then the statements which are present in the final else path are executed.
- Syntax:
If (expression 1)
{
Statement 1;
Statement 2;
}
else if (expression 2)
{
Statement 3;
Statement 4;
}
else
{
Statement 5;
Statement 6;
}
Note: when we use math.h in our program we have to compile it by gcc filename.c -lm
Switch Statement in C
- In C programming, the switch statement is a powerful control flow mechanism that enables you to select one code block to execute from multiple possibilities.
- It’s a versatile alternative to lengthy if-else if-else chains, especially when dealing with a single variable or expression with discrete values.
Syntax and How It Works:
switch (expression) {
case constant1:
// code to be executed if expression == constant1;
break;
case constant2:
// code to be executed if expression == constant2;
break;
// ... more cases
default:
// code to be executed if expression doesn't match any case
}
- Expression Evaluation: The switch statement begins by evaluating the expression inside the parentheses.
- Case Matching: The result is then compared to the values in each case label.
- Execution: If a match is found, the statements following the matching case label are executed until a break statement is encountered.
- Default (Optional): If no match is found, the statements under the default label (if present) are executed.
Switch statement program to find areas of different shapes
#include <stdio.h> int main() { int choice; // 1. Display menu options printf("Menu:\n"); printf("1. Find the area of a circle\n"); printf("2. Find the area of a rectangle\n"); printf("3. Find the area of a triangle\n"); printf("4. Exit\n"); // 2. Get user's choice printf("Enter your choice: "); scanf("%d", &choice); // 3. Use switch to handle different choices switch (choice) { case 1: // Case for circle float radius; printf("Enter the radius of the circle: "); scanf("%f", &radius); printf("The area of the circle is: %f\n", 3.14159 * radius * radius); break; case 2: // Case for rectangle float length, width; printf("Enter the length and width of the rectangle: "); scanf("%f %f", &length, &width); printf("The area of the rectangle is: %f\n", length * width); break; case 3: // Case for triangle float base, height; printf("Enter the base and height of the triangle: "); scanf("%f %f", &base, &height); printf("The area of the triangle is: %f\n", 0.5 * base * height); break; case 4: // Case for exit printf("Exiting...\n"); break; default: // Default case for invalid input printf("Invalid choice!\n"); } return 0; }
Output:
Menu:
1. Find the area of a circle
2. Find the area of a rectangle
3. Find the area of a triangle
4. Exit
Enter your choice: 1
Enter the radius of the circle: 5
The area of the circle is: 78.539750
FAQs related to conditional statement in C:
How do I use the if statement in C to check a single condition?
The if statement in C evaluates a condition enclosed in parentheses.
If the condition is true (non-zero), the code block within curly braces is executed.When should I use the if-else statement in C?
Use the if-else statement when you need to choose between two alternative actions based on a condition.
The if block executes if the condition is true, and the else block executes if the condition is false.Can I use if statements inside other if statements in C?
Yes, you can nest if statements within each other to handle more complex decision-making scenarios.
The inner if statement is only evaluated if the outer if statement’s condition is true.What is the difference between an else-if ladder and a switch statement in C?
i) Both else-if ladders and switch statements handle multiple conditions. However, else-if ladders are more flexible when conditions involve ranges or complex expressions.
ii) Switch statements are preferred when you have a single variable to test against multiple discrete values.How does the break statement work in a switch statement?
i) The break statement is crucial within each case of a switch. Without it, execution will “fall through” to the next case, potentially leading to unintended results.
ii) The break statement causes the switch block to terminate immediately when a matching case is found.