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

Load catalogue from file, Implement a menu driven real estate catalogue sys...

Implement a menu driven real estate catalogue system that allows users to perform various catalogue maintenance and search tasks.  You are only allowed to use the C programming la

Program for swapping the two integers, Program for Swapping the Two Integer...

Program for Swapping the Two Integers                 For example, Swapping the two value                                 int x=4,y=6,t;                                 t

Pointer, void main() { int *p, *q, i ; p=(int*)100; q=(int*)200; i=q-p; pr...

void main() { int *p, *q, i ; p=(int*)100; q=(int*)200; i=q-p; printf("%d",i); } }

Explain type casting, Type Casting Implicit type conversions, as allowe...

Type Casting Implicit type conversions, as allowed by the language, can lead to errors creeping in the program if care is not taken. Thus, explicit type conversions may be used

Android - iphone mobile game, I am seeking a developer to create a mobile g...

I am seeking a developer to create a mobile game for me. I want an endless runner game with simple controls. The game needs to be a 3D game in a 2D view. The graphics and animation

Program to calls to echo the typed characters, Write a main program that us...

Write a main program that uses these system calls to echo the typed characters. The pseudo code will look something like: void traphandler ()  {    if (R0 == 0) { // read sys

Define the logical operators in c language, Define the Logical Operators in...

Define the Logical Operators in c language? The Logical operators deals with the ways the relationships are able to connected. C has the following three logical operators

I want packet capture analysis, I want Packet capture analysis Project D...

I want Packet capture analysis Project Description: Need assistance in debugging some packet capture Skills required is C Programming

Decoding, Smugglers are becoming very smart day by day. Now they have devel...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

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