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

Define parameter passing mechanism - computer programming, Define the Param...

Define the Parameter Passing Mechanism - Computer Programming? The Parameters are syntactically identifiers and they are used within the body of the function and sometimes the

Program for tube challenge route, 1. Introduction The Tube Challenge is ...

1. Introduction The Tube Challenge is a Guinness World Records challenge that tests both the physical and mental abilities of the person trying to break it. The main components

I have to create three batch files, I have to create a bank atm machine usi...

I have to create a bank atm machine using MS-DOS. I am struggling to create LIST ALL ACCOUNTS ROLL-BACK TRANSACTIONS ACCESS AN ACCOUNT

Describe url and hostname part, Describe URL and hostname part? The hos...

Describe URL and hostname part? The hostname part of the URL should be a valid Internet hostname. It can also be an IP address like as 204.29.207.217 or 128.235.252.184. The

Develop a program on behavior of hospital personnel, A psychologist is inte...

A psychologist is interested in learning about the voting behavior of college students.  (4 points for each part.)Design a study which would yield data on this topic. a. state y

Write a program for random number generator, * Comments in your code are re...

* Comments in your code are required * Main Program Operation:  # Your program should first prompt the user for an integer to seed the random number Generator. "Enter an seed

Explain the comma operator- computer programming, Explain the Comma Operato...

Explain the Comma Operator- Computer Programming? The comma has two utilization and the most common use is as a parameter separator for data types and function parameter lists. H

Write a program to draw the truck , Write a program to draw the truck follo...

Write a program to draw the truck following figure. The program should buttons to provide When the truck runs left wheels must rotate in the correct direction. Rotating whe

Java, How to run java program?

How to run java program?

Mathematical expression of linear programming problem, A Consultant Dietici...

A Consultant Dietician provides diet and nutritional advice to clients. A client's diet requires that all the food s/he eats come from one of the four "basic food groups" (chocolat

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