FIFA-2022 Career Guide Free Tutorials Go to Your University Placement Preparation 
0 like 0 dislike
904 views
in RTU/BTU B.Tech(CSE-I & II Sem) Computer Programming Lab by Goeduhub's Expert (7.6k points)
Programs to understand nested if-else statement and switch statement.

1 Answer

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited by
 
Best answer

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

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.

Best Data Science Courses on Datacamp
Best Data Science Courses on Coursera
Best Data Science Courses on Udemy
Best Data Science Courses on Pluralsight
Best Data Science Courses & Microdegrees on Udacity
Best Artificial Intelligence[AI] Courses on Coursera
Best Machine Learning[ML] Courses on Coursera
Best Python Programming Courses on Coursera
Best Artificial Intelligence[AI] Courses on Udemy
Best Python Programming Courses on Udemy

 Important Lists:

Important Lists, Exams & Cutoffs Exams after Graduation PSUs

 Goeduhub:

About Us | Contact Us || Terms & Conditions | Privacy Policy ||  Youtube Channel || Telegram Channel © goeduhub.com Social::   |  | 

 

Free Online Directory

...