Conditional Statement: IF, IF Else and nested in C program DeveloperIndian
C Conditional Statements #clang #if_else #
Conditional statements are crucial in C programming. They control the flow of execution based on specified conditions. This guide explains the if
, if-else
, and nested if
statements with proper syntax and examples.
The if
statement executes a block of code only if a given condition evaluates to true
.
if (condition or expression) {
/* block of code */
}
#include <stdio.h>
int main() {
int a = 15;
if (a < 20) {
if (a < 10)
printf("a is less than 10\n");
else if (a == 10) // Fixed assignment operator error (a=10 to a == 10)
printf("a is equal to 10\n");
else
printf("a is greater than 10\n");
}
printf("Value of a is: %d\n", a);
return 0;
}
a
with the value 15
.if
statement checks if a < 20
, which is true
.if-else
block evaluates further:
a < 10
, prints "a is less than 10"
.a == 10
, prints "a is equal to 10"
(fixed comparison error)."a is greater than 10"
.if-else
statement allows decision-making in C.if
statements help evaluate multiple conditions.if
and else
must be in lowercase.