Nested if in C Language

Hello friends how are you, today in this blog i will teach you what is nested if statement, syntax of nested if , how nested if statement works and many programs using nested if statement in a very simple way.

Let's start

👉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
*/

Request:-If you found this post helpful then let me know by your comment and share it with your friend.

If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.

If you have not subscribed my website then please subscribe my website.

Try to learn something new and teach  something new to other.

Thanks.


Post a Comment

0 Comments