We can write if condition with in another if-condition, it is called nested-if statement.
Syntax:-
if(cond A){
if(cond 1 )
{
statement 1
}
else
{
statement 2
}
}
else
{
if(cond 2)
{
statement 3
}
else
{
statement 4
}
}
Note:-
1.Placing an if structure in a if structure is known as a Nested if statement.
2.First it checks the condition A.
3.If the condition A is true it's again checks condition 1.
4.If the condition 1 is true it executes true statement and then goes to next statement.
5.If the condition 1 is false it goes to next statement .
6.If the condition A is false it checks the condition 2.
7.If the condition 2 is true it executes true statement and then goes to next statement .
8.If condition 2 is false it executes the false statement and then goes to next statement.
2.First it checks the condition A.
3.If the condition A is true it's again checks condition 1.
4.If the condition 1 is true it executes true statement and then goes to next statement.
5.If the condition 1 is false it goes to next statement .
6.If the condition A is false it checks the condition 2.
7.If the condition 2 is true it executes true statement and then goes to next statement .
8.If condition 2 is false it executes the false statement and then goes to next statement.
Flow Chart:-
Examples:-
* Write a program to print student details and find out the total ,avg, and grade?
#include<stdio.h> #include<conio.h> void main() { int sno,c,cpp,java,tot; float avg; char sname[20]; clrscr(); printf("enter sno:"); scanf("%d",&sno); printf("enter sname:"); scanf("%s",&sname); printf("enter c,cpp,java marks:"); scanf("%d%d%d",&c,&cpp,&java); tot=c+cpp+java; avg=(float)tot/3; printf("sno is:%d\n",sno); printf("sname is:%s\n",sname); printf("c marks is:%d\n",c); printf("cpp marks is:%d\n",cpp); printf("java marks is:%d\n",java); printf("total marks is:%d\n",tot); printf("avarage marks is:%2.2f\n",avg); if(c>=40 && cpp>=40 && java>=40) { if(avg>=75) printf("A grade"); else if(avg>=60) printf("B grade"); else if(avg>=50) printf("C grade"); else if(avg>=40) printf("D grade"); } else printf("Fail"); getch(); } |
Output:-
enter sno:1214 enter sname:prasad enter c,cpp,java marks:78 86 53 sno is:1214 sname is:prasad c marks is :78 cpp marks is:86 java marks is:53 total marks is:217 average marks is:72.33 B grade |
* Write a program to check whether the given Username & Password.
#include<stdio.h> #include<conio.h> void main() { char username; int password; clrscr(); printf(“USERNAME:”); scanf(“%c”,&username); printf(“PASSWORD:”); scanf(“%d”,&password): If(username==’a’) { If(“password==12345) { printf(“Login Successful”); } else { printf(“Password is incorrect, Try again”); } } else { printf(“Username is incorrect, Try again”); } getch(); } |
Output:-
USERNAME:a PASSWORD:1785 Password is incorrect,Try again |
USERNAME:ert PASSWORD:236 Username is incorrect,Try again |
USERNAME:a PASSWORD:12345 Login Successful |
___________________________________________________________________________________
___________________________________________________________________________________
_____________________________________________
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Post a Comment