Intelle
Learn
CONDITIONAL
STATEMENTS
INTELLE
LEARN
What is a Conditional
Statement
make decisions in C?
A conditional statement in C programming is used to
based on certain conditions. It allows the
program to execute a block of code only when a specific
condition is true. If the condition is false, the program can
execute another block of code or skip the conditional
block entirely.
C provides several types of conditional statements:
• if statement
• if-else statement
• else if ladder
• nested if statement
• switch statement
1.THE IF STATEMENT
The if statement is the simplest form of a conditional
statement. It checks a condition, and if the condition is
true, the block of code associated with the if statement is
executed. If the condition is false, the program skips the
Syntax:
block.
if (condition) {
// code to be executed if the condition is true
}
Exampl
e: <stdio.h>
#include
int main() {
int number = 10; Outpu
if (number > 0) { t: is
The number
printf("The number is positive.
In this example, the condition number > 0 is
positive.\n"); true, so the message "The number is positive."
} is printed.
return 0;
}
2. THE IF-ELSE STATEMENT
The if-else statement allows for two blocks of code: one
that executes if the condition is true, and one that
executes if the condition is false.
Syntax:
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Exampl
e:<stdio.h>
#include
int main()
{
int number = -5; Outpu
if (number > 0) { t: is
The number
printf("The number is negative.
Here, since number is less than zero, the program
positive.\n");
will output "The number is negative."
}
else
{
printf("The number is
negative.\n");
}
3. THE IF ELSE LADDER
STATEMENT
When there are multiple conditions to check, the if else
ladder is used. It allows for multiple conditions to be
checked one after another.
Syntax:
if (condition1) {
// code to be executed if condition1 is true
}
else if (condition2)
{
// code to be executed if condition2 is true
}
else
{
// code to be executed if all conditions are false
}
Exampl
e:
#include <stdio.h>
int main() {
int number = 0;
{
if (number > 0)
Outpu
printf("The number is positive.\n");
}
else if (number < 0) t: is
The number
{ In this case, sincezero.
number is zero, the program will output
printf("The number is negative.\n");
} "The number is zero."
else
{
printf("The number is zero.\n");
}
return 0;
}
4. NESTED IF STATEMENTS
A nested if statement is when one if or else if statement is
placed inside another if or else if. It allows for more
complex decision-making.
Syntax:
if (condition1)
{
// code to be executed if condition1 is true
if (condition2)
{
// code to be executed if condition2 is also true
}
}
Exampl
e:<stdio.h>
#include
int main() {
int number = 10;
Outpu
if (number > 0) {
printf("The number is positive.\ t: is
The number
n"); positive.
Here, the program The number
will first check if is
the number is positive. If
if (number % 2 == 0) {
true, it will then check if the number is even. The output will
printf("The number is even.\ even.
be:
n");
}
}
return 0;
}
Thank
you!!
From:
Intellelearn