If Statement In C
- In C programming language, the if statement is used to execute a block of code only if a condition is true.
- The if in C is the most simple decision-making statement. It consists of the test condition and if a specify block or body. the given condition is true only then the if block will be executed.The syntax of the if statement is
Syntax:
if(condition)
{
// if body
// Statements to be execute if condition is true
}
{
// if body
// Statements to be execute if condition is true
}
Example:
#include <stdio.h>
int main()
{
int number = 10;
if (number > 0)
{
printf("The number is positive.\n");
}
return 0;
}
int main()
{
int number = 10;
if (number > 0)
{
printf("The number is positive.\n");
}
return 0;
}
Program:
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("%d is even.\n", number);
}
else
{
printf("%d is odd.\n", number);
}
return 0;
}
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("%d is even.\n", number);
}
else
{
printf("%d is odd.\n", number);
}
return 0;
}
Output:
Enter an integer: 3
3 is odd.
3 is odd.