Nested if in C programming with examples


Syntax


1.Nested means one inside another so one if inside another if is called nested if.
2.In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true

Example 1 

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=10;
if(x>5)//true
{
  if(x<5)//false
     {
     printf("Hello");
     }
  printf("Hi");
}
}
/*
### output ###
Hi
*/

Example 2

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=10;
if(x>5)//true
{
  if(x>5)//true
     {
     printf("Hello");
     }
  printf("Hi");
}
}
/*
### output ###
HelloHi
*/

Example 3 

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=10;
if(x<5)//false
{
  if(x>5)//true
     {
     printf("Hello");
     }
  printf("Hi");
}
}
/*
### output ###
No output because outer if condition is false
*/

Example 4: Find greatest value in three number

#include<stdio.h>
int main()
{
 //variables declaration
 int no1,no2,no3;
 //taking user input
 printf("Enter first number\n");
 scanf("%d",&no1);
 printf("Enter second number\n");
 scanf("%d",&no2);
 printf("Enter third number\n");
 scanf("%d",&no3);
 //applying condition
 if(no1>no2)
 {
 	if(no1>no3)
 	{
 		printf("%d is greatest",no1);
 	}
 }
  if(no2>no1)
 {
 	if(no2>no3)
 	{
 		printf("%d is greatest",no2);
 	}
 }
  if(no3>no1)
 {
 	if(no3>no2)
 	{
 		printf("%d is greatest",no3);
 	}
 }
}
/*
###Output###
Enter first number
85
Enter second number
96
Enter third number
47
96 is greatest
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments