11.4 Conditional statements with the switch and case keywords


11.4 Conditional statements with the switch and case keywords

C# has made one efficient change for the switch clause to accept strings in addition to integral expressions. The switch statement in Java can only accept the byte , short , char , and int primitive types.

Examine the following program which uses switch to compare a string (ignore the goto statement for now, it will be explained later).

 1:  using System;  2:  3:  class TestClass{  4:    public static void Main(string []args){  5:      string s = args[0];  6:  7:  switch(s)  {  8:            case "jam" : Console.WriteLine("BUTTER");  9:  goto case "butter";  10: 11:            case "cake": Console.WriteLine("CAKE"); 12:  break;  13: 14:            case "bug" : Console.WriteLine("BUG"); 15:  break;  16: 17:            case "fly" : Console.WriteLine("FLY"); 18:  break;  19: 20:            default    : Console.WriteLine("DEFAULT"); 21:  break;  22:      } 23:    } 24:  } 

Output:

 c:\expt>test bug BUG 

Note that capitalization is taken into consideration when comparing strings using switch . Check out the output when I used a different command line argument:

 c:\expt>test BUG DEFAULT 

Another change is that you must end each case block with a break; statement. Java and C/C++ have this 'fall through' behavior in which if you omit the break; the subsequent statements in the next case block actually execute all the way down until a break; is encountered (or the switch block ends).

In C#, you must end your case blocks either with a break statement or a goto statement. Otherwise there will be a compiler error. [6]

[6] I believe the removal of this fall-through behavior was to prevent careless programmers from bugging up their programs. Some Java programmers always forgot to insert the break statement after each case block when it really was meant to be there.

With the removal of this 'fall through' feature, are we condemned to write repeated code in multiple case blocks? Definitely not! How, then, do we force the execution of multiple case blocks for a particular situation? The solution is to use the goto keyword to jump from one case block to another.

Let's use another command line parameter and examine the output:

 c:\expt>test butter BUTTER FLY 

What has happened is that the case "butter" block (lines 8 “ 9) executes and prints out BUTTER). On line 9, the flow of execution meets the goto statement: goto case "fly"; . The flow of execution jumps to the case "fly" block (lines 17 “ 18) and carries on executing from there (thereby printing out ' FLY ').

Even if you want your program to flow from one case block to the next consecutive case block, you will need to use the goto keyword to accomplish that (see section 11.6).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net