It is used to perform operations according to a trueness or falseness of some condition.
Sometimes we want a set of instructions to be executed if certain condition is satisfied and an entirely different set of instructions to be executed if the condition does not fulfil. This kind of situation is dealt in C language using a decision control instruction.
if(condition){
//if true
}
if(condition){
//if true
}
else {
//if not true
}
if(condition 1){
//if true
}
else if(condition 2{
//if true
}
.
.
.
else {
// no condition is true
}
#include <stdio.h>
int main()
{
int age=0;
printf("Enter your age\n");
scanf("%d",&age);
if(age>=18){
printf("You are able to vote");
}
else {
printf("You are not able to vote \n try next year");
}
return 0;
}
#include <stdio.h>
int main()
{
int age=0;
printf("Enter your age\n");
scanf("%d",&age);
if(age>=18){
printf("You are able to vote");
}
if else(age>=10){
printf("you can apply for voter id but can't vote yet");
}
else {
printf("You are not able to vote \n try next year");
}
r eturn 0;
}
#include <stdio.h>
int main(){
int val;
printf("Put 1 if you have passed both exam \n put 2 if you have passed only maths \n put 3 is you have only passed science: ");
scanf("%d",&val);
if(val == 1){
printf("Congratulations you have passed both the exams your gift is Rs. 45");
}
else if(val == 2){
printf("Congratulations you have passed only in maths exam your gift is Rs. 15");
}
else if(val == 3){
printf("Congratulations you have passed only in science exam your gift if Rs. 15");
}
else{
printf("Your input is incorrect \n Put 1 if you have passed both exam \n put 2 if you have passed only maths \n put 3 is you have only passed science: ");
scanf("%d",&val);
}
return 0;
}