Skip to main content

Nested if Statements In C Programming

NESTED if STATEMENTS:

In simple words an if statement inside another if statement is called Nested if statement.

Syntax:

if(condition 1)
{
if(condition 2)
{code if condition 1 and 2 both are satisfied;}
}

Example Code:

Write a program to find whether the number entered by the user lies in a particular range.
Ans:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
getch();
printf("\n enter the number ");
scanf("%d",&n);
if(a>0)
{
if(a<18)
{
printf("\n the entered number is in the given range");
}
else

printf("\n the entered number is not in the given range");
}
}
else
{
printf("\n the number is not in the given range);
}
getch();
}
The above code is written in code blocks

Output:

enter the number
17
the entered number is in the given range

Explanation of the above Code:

  • In the above code we have declared an variable of data type integer ('n), the number entered by the user will be stored in this variable.
  • After accepting the number from the user we store it in n by using the scanf statement.
  • Then in the first step of nested loop we compare whether the number entered by the user is greater than 0 or not. 
  • If it is not greater than 0 then the statement written in the outer else statement gets printed.
  • If it is greater than 0 then we enter the outer if statement.
  • After entering the outer if statement we check the condition of inner if statement i.e. (n<18), if this condition is true then the statement written inside the inner if statement gets printed.
  • If the condition of the inner if statement is false the statement written inside the else statement gets printed.

Equal to Operator(==):

When ever we want to compare two values to check whether they are equal we use the equal to operator.

Syntax:

if(a==b)

{

code if both a and b are equal;
}

Example Code:

A program to check whether a number is odd or even 
Ans.
#include<stdio.h>
void main()
{
int a,b;
printf("\n enter the number");
scanf("%d",&a);
b=a%2;
if(b==0)
{
printf("\n the number is even");
}
else 
{
printf("\n the number is odd");
}
getch();
}

Output:

enter the number
6
the number is even

Explanation of the above code:

The above code works in the following manner after taking input we are checking what is the remainder of the number when divided by 2 if it is 0 then the number is divisible by 2 i.e it is even and if the remainder is 1 the number is odd. As 6%2 is equal to 0 so the code in the if statement gets executed.

Not equal to Operator (!=):

As the name suggest the operator checks whether a value is not equal to other, its use is similar to that of equal to operator.

Syntax:

if(a!=b)
{code if both values are not equal;}

Example Code:

To verify whether a four digit password and its confirmation are equal or not.
Ans.
#include<stdio.h>
void main()
{
int a,b;
clrscr();
printf("\n enter the four digit password");
scanf("%d",&a);
printf("\n enter the password again");
scanf("%d",&b);
if(a!=b)
{
printf("\n you have entered wrong password");
}
else
{
printf("\n verification successful");
}
getch();
}

Output:

enter the four digit password
1234
enter the password again
1324
you have entered wrong password

Explanation of the above code:

In the above code we have taken the input of the password twice to check whether the password entered by user is correct or not as the first password is 1234 which is not equal to the password which is entered again i.e 1324 hence the statement 'you have entered wrong password '
gets printed.

Conclusion:

Hence we have learned successfully how to execute Nested Conditional statement.

If you want to study the further topic of switch case click on the link given below
https://www.codingninjaa.com/2020/05/switch-case-in-c.html

If you have not learned the first part of conditional statement you can click on the link below

Comments

Popular posts from this blog

C Programming Basics

C PROGRAMMING BASICS: 1. Introduction to C language: C language was invented in Bell Labs by Dennis Ritchie between 1972-73. C language is a case sensitive language i.e the input given to it varies according to upper and lower case letters. It is executed using binary form data such as (0,1). By using binary data the speed of executing a code increases. 1.1 Header Files: Header files are those files which contain some predefined functions in it which we are going to use in our code. Every header file is accessed by appending an extension ".h". Example:               #include<stdio.h>                                 ↓                    ⇡          Tells computer to                  header file  with           include the header                 extension .h          file                                     T here are many header files for different functions such as the following:          <conio.h> <math.h> <string.h

How to start programming for beginners

HOW TO START PROGRAMMING: 1. What is Programming? Programming is a way of communicating with the computer and giving it set of instructions to do the work as per our needs. Example: While calculating something with a calculator what has to be done according to the input given to it is regulated by a program. Programming can also be used to build a software to determine whether a particular number is prime or not. 2. Can anyone learn Programming ? The answer to this question is simple YES anyone with basic knowledge of maths and some common sense can master programming.  The most amazing example of this is Sundar Pichai the CEO of Google he Studied metallurgical engineering at IIT Kharagpur. So if you love it you can do it. 3. Why Should I learn Programming? If you have that passion to solve a problem and enjoy twisting your mind then this is for you else you should leave programming right now. The other important Factor is Money if you become a m

Basic C Programming Questions

Basic Coding Questions: Exercise 1: Q1. Write a programme to calculate area and perimeter of rectangle by accepting its length and breadth. Ans: #include<stdio.h> #include<conio.h> void main() { float a,p,l,b; clrscr(); printf("\n enter the length and breadth of rectangle"); scanf("%f%f",&l,&b); a=l*b; p=2*(l+b); printf("\n the area of rectangle is %f and the perimeter is %f",a,p); getch(); } The above image code is written in code blocks Output: enter the length and breadth of rectangle 20(press enter) 10 (press enter) the area of rectangle is 200 and the perimeter is 60 Explanation of the above code: Declaring of variables in data type float since length and breadth cannot always be integers. Taking input of length and breadth together by using scanf function. Substituting the values of length and breadth stored in l and b