Recent

recentposts

Switch Statement

 Switch Statement 

 The switch statement is similar to if-else but only applicable to int and char datatype and all the probable outcomes are considered by defining different cases.

C++ provides a multiple-branch selection statement known as switch. This selection statement successively tests the value of an expression. When a match is found, the statements associated with that constant are executed.

Syntax:

switch( condition )

{

case outcome1 : statement1 ;

                break ;

case  outcome2: statement2 ;

                break ;

default : statement of default ;

                break ;

}

 

Switch



Program to find whether the number input by user is completely divisible by 3 or not .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;                                

int numb, remains ;


cout<<" Enter the number = " ;

cin>>numb ;


remains = numb%3 ;


switch( remains )

{

case 0: cout<<" Number is completely divisible " ;

break ;


default : cout<<" Not completely divisible " ;

break;

}


getch() ;

}

 

Program to find the area of triangle , square, rectangle and circle .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr();


int option ;

float base, height, side, length , width , radius ;

float area ;


cout<<" Choose the following choices to calculate the areas of different shapes " ;

cout<<"\n 1- For Triangle \n 2- For Square \n 3- For Rectangle \n 4- For Circle" ;


cout<<"Enter your choice = " ;

cin>>option ;


switch ( option )

{

case 1: cout<<" Enter the base value of Right angle triangle = " ;

        cin>>base ;

    cout<<" Enter the height value of Right angle triangle = " ;

        cin>>height ;

        area = (1/2)*base*height ;

        cout<<"The area of Right triangle is = "<<area ;

        break ;


case 2: cout<<" Enter the side length of square = " ;

        cin>>side ;

        area = side*side ;

        cout<<" The area of Square is = "<<area ;

        break ;


case 3: cout<<" Enter the length of rectangle = " ;

        cin>>length ;

    cout<<"Enter the width of rectangle = " ;

        cin>>width ;

        area = lenght*width ;

        cout<<" The area of Rectangle is = "<<area ;

        break ;


case 4: cout<<" Enter the radius of circle = " ;

        cin>>radius ;

        area = 3.14*radius*radius ;

        cout<<" The area of Circle is = "<<area ;

        break ;


default : cout<<" Your choice is improper , please make a proper choice from above available options " ;

         break ;

}


getch() ;

}

 

Program to find whether vowel or consonants is entered by user .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ; 

char ch ;


cout<<" Enter a letter = " ;

cin>>ch ;


switch ( ch )

{

case ‘a’ : cout<<" It is  a vowel " ;

break ;


case ‘A’ : cout<<" It is  a vowel " ;

break ;


case ‘e’ : cout<<" It is  a vowel " ;

break ;


case ‘E’ : cout<<" It is  a vowel " ;

break ;


case ‘i’ : cout<<" It is  a vowel " ;

break ;


case ‘I’ : cout<<" It is  a vowel " ;

break ;


case ‘o’ : cout<<" It is  a vowel " ;

break ;


case ‘O’ : cout<<" It is  a vowel " ;

break ;


case ‘u’ : cout<<" It is  a vowel " ;

break ;


case ‘U’ : cout<<" It is  a vowel " ;

break ;


default : cout<<" It is a consonants " ;

    break ;

}


getch() ;

}

 

Note :-  We can use fall through in switch which reduce the code length , use the following method .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;

char ch ;


cout<<" Enter the character = " ;

cin>>ch ;


switch ( ch )

{

case ‘a’:

case ‘A’:

case ‘e’:

case ‘E’:

case ‘i’:

case ‘I’:

case ‘o’:

case ‘O’:

case ‘u’:

case ‘U’: cout<<"It is vowel " ;

/* This is called fall through in switch.   */

    break ;


default : cout<<" It is a consonants " ;

   break ;

}


getch() ;

}


Program to make a calculator .

#include<iostream.h>

#include<conio.h>


void main(){


clrscr() ;

float numb1, numb2, output ;

char opt ;


cout<<" The operation that can be performed are + , - , * , / , % " ;

cout<<"\n Enter the first number = " ;

cin>>numb1 ;

cout<<"\n Enter the operator=  " ;

cin>>opt ;

cout<<"\n Enter the second number = " ;

cin>>numb2 ;


switch ( opt )

{

case ‘+’ : output = numb1 + numb2 ;

break ;


case ‘-’ : output = numb1numb2 ;

break ;


case ‘*’ :output = numb1 * numb2 ;

break ;


case ‘/’ : output = numb1 / numb2 ;

break ;


case ‘%’ : output = numb1 % numb2 ;

break ;


default : cout<<" Invalid operator chosen " ;

    break ;

}


cout<<"\n The output is = "<<output ;

getch() ;

}


___________________________________________________________________________________

SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment

Please give your valuable suggestions