By default, a C program executes statements sequentially. Conditional statements alter this flow, enabling the program to choose a path based on the evaluation of a logical expression. In C, any non-zero value is considered true, and zero is considered false.
1. The if
Statement
The if
statement is the simplest decision-making statement.
It executes a block of code only if a given condition is true.
· Syntax:
C
if (condition) {
// statements to execute if condition is true
}
Example
This program checks if a user-entered number is positive.
C
#
int main() {
int number;
printf(
"Enter an integer: ");
scanf(
"%d", &number);
// If the number is greater than 0, the condition is true
if (number >
0) {
printf(
"You entered a positive number: %d\n", number);
}
printf(
"This statement always executes.\n");
return
0;
}
2. The if-else
Statement
The if-else
statement provides an alternative path for when the
condition is false. It executes one block of code if the condition is true and
a different block if it's false.
· Syntax:
C
if (condition) {
// block to execute if condition is true
}
else {
// block to execute if condition is false
}
Example
This program checks if a number is even or odd.
C
#include <stdio.h>
int main() {
int number;
printf(
"Enter an integer: ");
scanf(
"%d", &number);
if (number %
2 ==
0) {
printf(
"%d is an even number.\n", number);
}
else {
printf(
"%d is an odd number.\n", number);
}
return
0;
}
3. The if-else-if
Ladder
This structure
(also known as an else-if ladder) is used to decide among multiple options.
As soon as one of the if
or else if
conditions is found to be true, its associated block
is executed, and the rest of the ladder is skipped.
· Syntax:
C
if (condition1) {
// block 1
}
else
if (condition2) {
// block 2
}
else
if (condition3) {
// block 3
}
else {
// final else block (optional)
}
Example
This program demonstrates a simple grading system.
C
#include <stdio.h>
int main() {
int score;
printf(
"Enter your score (0-100): ");
scanf(
"%d", &score);
if (score >=
90) {
printf(
"Grade: A\n");
}
else
if (score >=
80) {
printf(
"Grade: B\n");
}
else
if (score >=
70) {
printf(
"Grade: C\n");
}
else
if (score >=
60) {
printf(
"Grade: D\n");
}
else {
printf(
"Grade: F\n");
}
return
0;
}
4. The switch
Statement
The switch
statement is an alternative to a long if-else-if
ladder. It evaluates a variable or expression and
checks it against a series of constant values specified in case
labels.
· Key Concepts:
o case
: The value must be a constant integer or character.
o break
: This is crucial. It terminates the switch
statement. Without it, execution will "fall
through" to the next case.
o default
: This optional block is executed if none of the case
values match the expression.
Example
This program takes a number from 1 to 7 and prints the corresponding day of the week.
C
#include <stdio.h>
int main() {
int day;
printf(
"Enter day number (1-7): ");
scanf(
"%d", &day);
switch (day) {
case
1:
printf(
"Sunday\n");
break;
case
2:
printf(
"Monday\n");
break;
case
3:
printf(
"Tuesday\n");
break;
case
4:
printf(
"Wednesday\n");
break;
case
5:
printf(
"Thursday\n");
break;
case
6:
printf(
"Friday\n");
break;
case
7:
printf(
"Saturday\n");
break;
default:
printf(
"Invalid input! Please enter a number between 1 and 7.\n");
break;
}
return
0;
}
Conditional statements in C allow your program to make decisions, executing different blocks of code based on whether a given condition evaluates to true (any non-zero value) or false (zero).