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

Difference between CMP and SUB, 1. CMP and SUB CMP is used for comparing 2 ...

1. CMP and SUB CMP is used for comparing 2 registers by subtraction them from one another, but answer is not saved, whereas SUB subtracts 2 registers and saves the answer.

Determine that the processes are iterative or recursive, Each of the follow...

Each of the following two procedures defines a method for adding two positive integers in  terms of the procedures inc, which increments its argument by 1, and dec, which decrement

C# not c++ programing help, Please write a program declaring a variable x a...

Please write a program declaring a variable x as the type int, and use the scanf function to input an integer to initialize the value of x. Then, your program will print the follow

Shell script to convert decimal no. to any other base no., Normal 0 ...

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

Maze-solving, For this assignment you will use the robot/maze simulation ex...

For this assignment you will use the robot/maze simulation example introduced in mini-assignments 0625ma-0627ma to develop a maze-solving program. The program will read a file cont

Write a function to draw backbone of the protein, Write a function drawCA(a...

Write a function drawCA(aname, coords) that uses the plot3 command to draw the C α backbone of the protein. When calling this function, you should pass in the aname and coords var

Write a program to draw a sketch of a cow, write a program to draw a cow. N...

write a program to draw a cow. Now, draw a cow standing amongst the grass, with (say) 500 blades of grass. Each blade of grass should be a separate random green line. Do all your d

Java packages, Expertsmind brings you unique solution in java assignments ...

Expertsmind brings you unique solution in java assignments Packages A Java packages is a process for planning Java sessions into namespaces just like the segments of Modu

Hack winrar password, how to make a program using vb?seriously i don''t kno...

how to make a program using vb?seriously i don''t know

Devlop personalized location based news service, The system you will develo...

The system you will develop should provide the following functionality to its users.     1.  Your system should allow the users to aggregate three different RSS feeds (they should

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