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

Sort wars, Explain each of the algorithms in a way that would be understand...

Explain each of the algorithms in a way that would be understandable to an intelligent person who is not familiar with programming. You should not use any code (or even pseudo code

Program to print a series, Write a program called Series that will print/di...

Write a program called Series that will print/display a series of numbers starting with 1 and ending with the last number that is less than 30. The series should include only odd n

Programming, Write a program that writes your name on the monitor ten times...

Write a program that writes your name on the monitor ten times. Write this program three times, once with each looping method.

Programming, hello i have about 15 hours the dead line I Need a program in...

hello i have about 15 hours the dead line I Need a program in c language that''s determine the router ip or the gateway ip of the current NIC example the router ip is: 192.168.1.1

Define difference among new & malloc?, Both malloc & new functions are util...

Both malloc & new functions are utilized for dynamic memory allocations & the basic difference is: malloc need a special "typecasting" while it allocates memory for eg. if the poin

If else, A small shop sells 280 different items. Every item is identified b...

A small shop sells 280 different items. Every item is identified by a 3 - digit code. All items which start with a zero (0) are cards, all items which start with a one (1) are swee

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