Definition:
The if-else statement is a decision-making statement in the C programming language.
It allows a program to execute one block of code when the condition is true and another block when the condition is false.
Syntax:
if(condition)
{
// statements
}
else
{
// statements
}
Explanation:
The condition inside the if statement is evaluated first.
If the condition is true, the statements inside the if block are executed.
Otherwise, the statements inside the else block are executed.
Explanation: This program accepts a number from the user. If the number is greater than zero, it displays Positive Number; otherwise, it displays Negative Number.
#include <stdio.h> #include <conio.h> main() { int Number; clrscr(); printf("Enter the Number : "); scanf("%d",&Number); if(Number > 0) { printf("\nPositive Number"); } else { printf("\nNegative Number"); } getch(); }
Output:
if-else statement.if-else statement.10.
#include <stdio.h> #include <conio.h> main() { int Number; clrscr(); printf("Enter the Number : "); scanf("%d",&Number); if(Number > 10) { printf("\nNumber is Greater than 10"); } else { printf("\nNumber is Less than or Equal to 10"); } getch(); }
10. If the entered number is greater than 10, it displays "Number is Greater than 10"; otherwise, it displays "Number is Less than or Equal to 10".