Forums » .NET » .NET »

advantage & disadvantage of switch case


Posted Date: 26 Nov 2008      Posted By:: Radha     Member Level: Silver    Member Rank: 0     Points: 1   Responses: 5



advantage & disadvantage of switch case



Responses

#321800    Author: Vanaja Chiluveru      Member Level: Silver      Member Rank: 0     Date: 26/Nov/2008   Rating: 2 out of 52 out of 5     Points: 4

In some languages and programming environments, a case or switch statement is considered easier to read and maintain than an equivalent series of if-else statements, because it is more concise.

However, when implemented with fall-through, switch statements are a frequent source of bugs among programmers new to the switch statement.


 
#321803    Author: Syed Shakeer Hussain      Member Level: Gold      Member Rank: 104     Date: 26/Nov/2008   Rating: 2 out of 52 out of 5     Points: 6

hi..
The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body as the following example:

int caseSwitch = 1;
switch (caseSwitch)
{
case 1:
Console.WriteLine("Case 1");
break;
case 2:
Console.WriteLine("Case 2");
break;
default:
Console.WriteLine("Default case");
break;
}
Control is transferred to the case statement which matches the value of the switch. The switch statement can include any number of case instances, but no two case statements can have the same value. Execution of the statement body begins at the selected statement and proceeds until the break statement transfers control out of the case body. A jump statement such as a break is required after each case block, including the last block whether it is a case statement or a default statement. With one exception, (unlike the C++ switch statement), C# does not support an implicit fall through from one case label to another. The one exception is if a case statement has no code.

If no case expression matches the switch value, then control is transferred to the statement(s) that follow the optional default label. If there is no default label, control is transferred outside the switch.

A switch statement is executed as follows:
2 The switch expression is evaluated and converted to the governing type.
3 If one of the constants specified in a case label in the same switch statement is equal to the value of the switch expression, control is transferred to the statement list following the matched case label.
4 If none of the constants specified in case labels in the same switch statement is equal to the value of the switch expression, and if a default label is present, control is transferred to the statement list following the default label.
5 If none of the constants specified in case labels in the same switch statement is equal to the value of the switch expression, and if no default label is present, control is transferred to the end point of the switch statement.


Thanks & Regards!
Syed Shakeer Hussain


 
#321805    Author: Shivshanker Cheral      Member Level: Gold      Member Rank: 25     Date: 26/Nov/2008   Rating: 2 out of 52 out of 5     Points: 6

<b> Advantages and Disadvantages </b>

In some languages and programming environments, a case or switch statement is considered easier to read and maintain than an equivalent series of if-else statements, because it is more concise. However, when implemented with fall-through, switch statements are a frequent source of bugs among programmers new to the switch statement.

A switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., Pascal, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.

In most languages, a switch statement is defined across many individual statements. A typical syntax is that the first line contains the actual word "switch" followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take.

Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats.

An optional special block is also allowed, which does not specify any value and which begins with the default keyword instead of the case keyword. If this block is present and if none of the values listed for any other block matches that of the control variable, program execution will jump to the statement following the default keyword.

The method of terminating a block is also of note. Typically, a break keyword is used to signal the end of the block. When encountered, this keyword causes program execution to continue with the first statement after the series of statements within the switch statement, thus completing execution of the switch statement. If no break keyword is present at the end of the block, in many languages program execution "falls through" to the code associated with the next block in the switch statement, as if its value also matched the value of the control variable. Notable exceptions include C#, in which fallthrough is not permitted unless the block is empty and all blocks must be terminated via a break, or by using another keyword.

Thanks
Shivshanker Cheral
"If you share your assets (money etc..) it will decrease, But if you share your knowledge it will increase!!! ".





 
#321845    Author: Deepika Haridas      Member Level: Gold      Member Rank: 8     Date: 26/Nov/2008   Rating: 2 out of 52 out of 5     Points: 6

A switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., Pascal, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.

In most languages, a switch statement is defined across many individual statements. A typical syntax is that the first line contains the actual word "switch" followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take.

Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats.

An optional special block is also allowed, which does not specify any value and which begins with the default keyword instead of the case keyword. If this block is present and if none of the values listed for any other block matches that of the control variable, program execution will jump to the statement following the default keyword.

The method of terminating a block is also of note. Typically, a break keyword is used to signal the end of the block. When encountered, this keyword causes program execution to continue with the first statement after the series of statements within the switch statement, thus completing execution of the switch statement. If no break keyword is present at the end of the block, in many languages program execution "falls through" to the code associated with the next block in the switch statement, as if its value also matched the value of the control variable. Notable exceptions include C#, in which fallthrough is not permitted unless the block is empty and all blocks must be terminated via a break, or by using another keyword.



Thanks & Regards,
Deepika - Sr. Editor
Webmaster, GujaratSpider
http://angeldeeps.blogspot.com/
"Experience is the name everyone gives to his mistakes"


 
#323986    Author: dotnet      Member Level: Silver      Member Rank: 0     Date: 01/Dec/2008   Rating: 2 out of 52 out of 5     Points: 0

http://en.wikipedia.org/wiki/Switch_statement

 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : WHAT IS "SIEBEL" AND WHAT IS THE UTILITY OF THIS?
Previous : how i can get current month and date only using sqlsever
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages

Subscribe to Subscribers
Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John

Online Members

srinivas
More...
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India