C Conditional Statements
Conditional statements in C allow a program to make decisions based on certain conditions. These statements control the flow of execution by checking if a specific condition is met. If the condition is true, a certain block of code executes; otherwise, another block runs.
In this tutorial, we will learn about different conditional statements in C with detailed explanations and examples.
Types of Conditional Statements in C
C provides the following conditional statements:
if
statement – Executes a block of code if a condition is true.if-else
statement – Executes one block if the condition is true, otherwise executes another block.if-else if-else
statement – Allows multiple conditions to be checked sequentially.switch
statement – Allows a variable to be tested against multiple values.
1. The if
Statement
The if
statement checks a condition, and if it evaluates to true, the code inside the block executes.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example: Check if a number is positive
In this example program, we will check if the value in variable num
is a positive number using if statement.
main.c
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive.\n");
}
return 0;
}
Explanation:
- The variable
num
is initialized with10
. - The
if
statement checks ifnum > 0
. - Since the condition is true, the message
"The number is positive."
is printed.
Output:
The number is positive.
Detailed tutorial on if statement.
2. The if-else
Statement
The if-else
statement executes one block of code if the condition is true, and another block if the condition is false.
Syntax:
if (condition) {
// Code executes if condition is true
} else {
// Code executes if condition is false
}
Example: Check if a number is even or odd
In this example program, we will check if the value in variable num
is an even number or odd number using if-else statement.
main.c
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Explanation:
- The variable
num
is assigned7
. - The condition
num % 2 == 0
checks if the number is even. - Since
7
is odd, theelse
block executes, printing"The number is odd."
.
Output:
Detailed tutorial on if-else statement.
The number is odd.
3. The switch
Statement
The switch
statement is used when a variable needs to be compared against multiple values.
Syntax:
switch (variable) {
case value1:
// Code executes if variable == value1
break;
case value2:
// Code executes if variable == value2
break;
default:
// Code executes if no case matches
}
Example: Display a day based on user input
In this example program, we will print the name of day based on the value in variable day
using switch statement.
main.c
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Explanation:
- The variable
day
is assigned3
. - The
switch
statement comparesday
with cases. - Since
day
is3
, “Wednesday” is printed.
Output:
Wednesday
Detailed tutorial on switch statement.