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 statements to define the constants, the problem description. The ...

the problem description. The order of the C Program should be as follows: Variables and constants Use #define statements to define the constants. Use arr

What are all of the implicit member functions of the class? , What are all ...

What are all of the implicit member functions of the class? Or what are all of the functions that compiler implements for us if we don't describe one? A: 1.      copy ctor

Area under curve., write a c progrm to find area under the cuve y=f(x)  ...

write a c progrm to find area under the cuve y=f(x)   #include float start_point, /* GLOBAL VARIABLES */ end_point, total_area; in

Set performance data and print tickets, Create a class  called  ticketSelli...

Create a class  called  ticketSelling that stores booking information of a single performance on a single day and sells the tickets of the performance. The class should include at

Differences between a pointer and a reference, Differences between a pointe...

Differences between a pointer and a reference 1.  A reference must always point to some object where as this restriction is not imposed on a pointer. e.g. int *pi = 0;

Write a constructor for a movie, Suppose we use Lists to represent a movie ...

Suppose we use Lists to represent a movie which has a title, cast and year. For example, the following list represents the movie, "Die Hardly", with the cast "Bruce Willing" and "T

ASCII, A string S is said to be "Super ASCII", if it contains the character...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''a''-''z'') and the asci

Crossoword puzzle, find wether specific word is inside a pool of word and w...

find wether specific word is inside a pool of word and what is the direction e.g. north

Arrays within a class, A r r a y s w i t h i n a c l a s s:...

A r r a y s w i t h i n a c l a s s: I t i s j u s t d ecl a r i n g o r c on s t ru c ti n g a d e r i v e d t

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