What is the switch statement, Programming Languages

Assignment Help:

What is the switch Statement?

This is a different form of the multi way decision that It is well structured, but can only be used in certain cases where:

1. Here, Only one variable is tested, all branches must depend on the value of that variable. The variable should be an integral type. (int, long, short or char).

2. Every possible value of the variable can control a single branc and a final, catch all, default branch may optionally be used to trap all unspecified cases.

The universal form of switch statement is

            switch(variable name)

            {

                        case constant 1            :           {

                                                                        block 1;

                                                                        break;

                                                            }         

 

                        case constant 2            :           {

                                                                        block 1;

                                                                        break;

                                                }

 

            .................

            .................

 

            default :                       {

                                                                        default block;

                                                                        break;

                                                            }

            }

The switch statement tests the value of a variable name against a list of  integer or character constants When a match is found the statements corresponding to that constant are executed and The switch differs from the if - else, as switch can only test for equality whereas if - else can do any type of relational & logical condition checking. Note that a body of the switch statement is a compound statement and the expression must evaluate to an integertype.

Once the expression is calculated, the control jumps to the appropriate case label and the statements corresponding to that case is executed until the break statement is reached and the break statement is used as an exit from the switch statement, otherwise the following case statements will get executed. If not any of the case matches, the control goes to the default statement and that particular statement gets executed.


Related Discussions:- What is the switch statement

Pattern matching program in xhtml, Part 1 Given the following Regular Exp...

Part 1 Given the following Regular Expressions, explain in detail the pattern accepted by each. Pay attention to all the characters being used. Your answers need not appear with

Perl program, Input to be in the form of a string of less than 256 characte...

Input to be in the form of a string of less than 256 characters. Assume that this character string (record) can only be of the form: Last Name, First Name, Box number, City, St

Laplace transforms, There actually isn't all that much to this section. All...

There actually isn't all that much to this section. All we are going to do now is work a quick illustration using Laplace transforms for a 3 rd order differential equation therefo

Create xml documents to contain well-defined information, The objectives of...

The objectives of this assignment are to test your ability to create XML documents to contain well-defined information. This assignment will be used further in Assignment 2, so ple

Cookies management with perl, One of the main strengths of the Perl program...

One of the main strengths of the Perl programming language are its powerful text manipulation features. In this assignment, you will put them to  use for writing a Perl program tha

Shell script toconvert all lowercase to uppercase characters, Normal 0 ...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Service oriented architectures in xml, question 1: In the opening lecture I...

question 1: In the opening lecture I spoke about general changes in business - flatter organizations, process orientation as opposed to functional silos, focus on supply chains, gr

Linux Lab Assignments, I have a linux lab class that consist of 1 lab assig...

I have a linux lab class that consist of 1 lab assignment per week for 5 weeks. It is an online virtual lab and is fairly easy, I just don''t have the time.

CMIS 102, Calculate the total price based on several key parts required to ...

Calculate the total price based on several key parts required to build a state-of-the-art gaming computer. The user will have the option of selecting different parts.

Program which prints odd or even according to user input, Note: You should ...

Note: You should pay attention on following good practices: Appropriate code comments(If you make any assumptions clearly comment them in the code) Indentation

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