If Else Condition in C
1.What is if else in C ?
Ans: The if-else statement is a decision-making statement that is used to decide whether the part of the code will be executed or not based on the specified condition (test expression). If the given condition is true, then the code inside the if block is executed, otherwise the code inside the else block is executed.
Simple Explanation :
In this image we can see that to buy things we give money. The shopkeeper will first check the note. If the note is original he will gives us the product we want. If the note is Duplicate he will not give the product.
If and else condition also works same. It will first check the condition and print the statement in if block or print the statement in the else block.
Working of if else :
Syntax :
if (condition)
{
// code executed when the condition is true
}
else
{
// code executed when the condition is false
}
Example:
1. Write a program to find that the number is positive numbers.
#include<stdio.h> // Header file
int main () // Main
{
int num;
printf("enter your favourite number");
scanf("%d",&num);
if(num%2==0) // Condition
{
printf("%d is even",num);
}
else
{
printf("%d is odd",num);
}
}
Output :
#include<stdio.h> // Header file
int main ( ) // Main
{
int a,b;
printf("enter your favourite number for a");
scanf("%d",&a);
printf("enter your favourite number for b");
scanf("%d",&b);
if(a==b) // Condition
{
printf("numbers are equal");
}
else
{
printf("numbers are unequal");
}
}
Output :
0 Comments