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

develop a calculator in masm, Part 1 Assignment:  develop a calculator...

Part 1 Assignment:  develop a calculator in MASM. Text chapters covered:  1 through 4, 5.4, 5.5, 6.3, 7.4 You will develop a "calculator" algorithm in MASM using reverse-

Explain member functions of a class, Member Functions of a Class A memb...

Member Functions of a Class A member function of the class is similar as an ordinary function. Its declaration in a class template must explain its return value as well as the

Custom scans and alerts think or swim, Custom Scans and Alerts Think or Swi...

Custom Scans and Alerts Think or Swim Project Description: I am seeking somebody to make me several Custom Scans and Alerts on Thinkorswim TOS Skills required are C++ Prog

A palindrome is a string that reads the same from both the e, submitting so...

submitting solutions in C language should not use functions from / as these files do not exist in gcc

Change to palindrome, A palindrome is a string that reads the same from bot...

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

#, YOU HAVE GIVEN THE MARKED UP PRICE OF 80% AND DISCOUNT PRICE OF 10% THEN...

YOU HAVE GIVEN THE MARKED UP PRICE OF 80% AND DISCOUNT PRICE OF 10% THEN FIND THE SELLING PRICE

What is object variable, The definition of an object(variable ): We can...

The definition of an object(variable ): We can explain a variable(set memory to the variable) in the following ways. e.g. double salary; int month; When more than o

Oop, define a class for student

define a class for student

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