Explain If else statement with example in c
C language
In C, the "if-else" statement is used to control the flow of a program based on a certain condition. It allows the program to execute different sets of instructions based on whether the condition is true or false.
The basic syntax of an "if-else" statement in C is as follows:
if (condition)
{
// code to be executed if the condition is true
}
else
{
// code to be executed if the condition is false
}
Here is an example to illustrate how the "if-else" statement works in C:
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("The number is positive.\n");
}
else if (num < 0)
{
printf("The number is negative.\n");
}
else
{
printf("The number is zero.\n");
}
return 0;
}
In this example, the program prompts the user to enter a number. Then, the "if-else" statement is used to check whether the number is positive, negative, or zero. If the number is positive, the program prints "The number is positive." If the number is negative, the program prints "The number is negative." If the number is zero, the program prints "The number is zero."
#codingdev
So guys this is a very simple statement You are a learn this code and I hope you like this post.
Thankyou
0 Comments