If statement(conditional statement)
Presented by MINAKSHI SHARMA
What is an if Statement?
Use if Statements
if statements allow you to make decisions in your program by executing code only when a specific condition is true.
Type of if
Simple if Syntax
{
// Code to execute if the condition is true
}
Example:-
int x = 10;
if (x > 5)
{
printf("x is greater than 5!");
}
If else Syntax
{
// Code if true
}
else
{
// Code if false
}
int x = 3;
if (x > 5)
{
printf("x is greater than 5!");
}
else
{
printf("x is 5 or less.");
}
Example
Nested If(if within if)�Syntax
if (num > 5)
{
// Outer condition
if (num % 2 == 0)
{ // Inner condition
printf("The number is greater than 5 and even.\n");
}
}
Example
if (condition1)
{
if (condition2)
{
// Code to execute if both conditions are true
}
}
ladder If Syntax
Example
if (condition1)
{
// Code if condition1 is true
}
else if (condition2)
{ // Code if condition2 is true
}
else
{ // Code if none are true
}
int x = 7;
if (x > 10)
{
printf("x is greater than 10");
}
else if (x > 5)
{ printf("x is between 6 and 10");
}
else { printf("x is 5 or less");
}
Greatest between three nos
Using Nested if
Using Ladder if
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 15;
if (a > b && a > c)
{
printf("a (%d) is the greatest.\n", a);
}
else if (b > c)
{
printf("b (%d) is the greatest.\n", b);
}
else { printf("c (%d) is the greatest.\n", c); }
return 0;
}
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 15;
if (a > b)
{ if (a > c)
{
printf("a (%d) is the greatest.\n", a);
}
else
{
printf("c (%d) is the greatest.\n", c);
}
}
else
{
if (b > c)
{
printf("b (%d) is the greatest.\n", b);
}
else
{
printf("c (%d) is the greatest.\n", c);
}
}
return 0;
}
thanks