Explain the switch construct, C/C++ Programming

Assignment Help:

The Switch Construct

The switch statement is a multi-way decision-making construct that tests an expression matches one of a number of constant values, and branches accordingly. The switch statement is another alternative to using nested if-else statements. The general format is as follows :

                switch(expression)

                 {

 

                                case value1 : statements;

                                                                                :

                                                                                :

                                                                   break;

 

                                case value2 : statements;

                                                                                :

                                                                                :

                                                                   break;

 

                                case value3 : statements;

                                                                                :

                                                                                :

                                                                   break;

 

                                default     : statements;

                                :                                                                                              

:

                 }

 

The expression must be declared in the parentheses, and the body of the switch statement must be enclosed in braces. The values with case should be constants. The expression is evaluated and compared with the values in cases; if it matches the statements under it are implemented till it encounters a break statement. If the value does not match with any of the case statements then the statements under the default label are implemented.

Omission of break takes control by the next case statements regarded whether the value of case matches or not. This can also be used constructively when similar operation has to be performed on a number of cases.

 

e.g.

 

                switch(ch)

                 {

 

                                case 'a' :

                                case 'e' :

                                case 'i' :

                                case 'o' :

                                case 'u' : cout << "Vowel";

default  : cout << " Consonant ";

                 }

 

In  the above example if the value of ch is any of a,e,i,o,u then they are printed as vowels , or else they are printed as consonants

 


Related Discussions:- Explain the switch construct

Define enumeration in computer programming, Define Enumeration in Computer ...

Define Enumeration in Computer Programming? Enumerated types enclose a list of constants that are able to be addressed in integer values. We can declare variables and types

Selection sort - c program, Selection sort - C program: Write a progra...

Selection sort - C program: Write a program to define a selection sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0;i

Operator overloading - c++ program, Operator overloading - c++ program: ...

Operator overloading - c++ program: Write a program in c to define operator overloading. class matrix{                   private :                 int x[2][2];

''c'' programme, Write a ''C'' program to accept any 3 digit integer number...

Write a ''C'' program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

Explain automatic variables, Automatic Variables Automatic variables ar...

Automatic Variables Automatic variables are variable which are explained within the functions. They lose their value when the function terminates. It can be accessed only in th

Declaring Integer variables with an example, How do you declare an integer ...

How do you declare an integer variable? Explain with an example.

Sdsds, Given an integer n and a permutation of numbers 1, 2 ... , n-1, n wr...

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd