Programs to understand nested if-else statement and switch statement.
Nested If : nested if-else statements, means you can use one if or else if statement inside another if or else if statement .
Syntax :
if( Condition 1) { // Executes when the Condition 1 is true if( Condition 2) { // Executes when the Condition 2 is true } } else { //Execute else part } |
---|
Example :
#include<stdio.h> #include<conio.h> void main() { int a = 10; int b = 20; if( a == 10 ) { if( b == 10 ) { printf("GoEduHub Welcomes you \n" ); } else { printf("Oh my GOD you have got wrong value of B "); } } else { printf("value of a is : %d\n", a ); printf("value of b is : %d\n", b ); } } |
---|
Output :
Oh my GOD you have got wrong value of B
Switch statement : switch statement is used when you have multiple possibilities for the if statement. Switch case will allow you to choose from multiple options.the switch case allows you to set the necessary statements for the user.At the end of each block it is necessary to insert a break statement because if the programmers do not use the break statement, all consecutive blocks of codes will get executed from every case onwards after matching the case block.
Syntax :
switch(variable) { case 1: //execute code break; case n: //execute code break; default: //execute code break; } |
---|
Example :
#include<stdio.h> #include<conio.h> void main() { int a; printf("Please enter a no numbers from 1 to 7 : "); scanf("%d",&a); switch(a) { case 1: printf("Its Sunday"); break; case 2: printf("Its Monday"); break; case 3: printf("Its Tuesday"); break; case 4: printf("its Wednesday"); break; case 5: printf("Its thursday"); break; case 6: printf("Its Friday"); break; case 7: printf("Its Saturday"); break; default : printf("YOU HAVE ENTERED WRONG NUMBER"); break; } } |
---|
Output :
Please enter a no numbers from 1 to 7 : 8
YOU HAVE ENTERED WRONG NUMBER
For more RTU/BTU II Sem Computer Programming Lab Experiments CLICK HERE