15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

loops ❘ OC173<br />

}<br />

switch (s)<br />

{<br />

case Suit::Heart:<br />

case Suit::Diamond:<br />

color = "Red";<br />

break;<br />

case Suit::Spade:<br />

case Suit::Club:<br />

color = "Black";<br />

break;<br />

default:<br />

color = "Unknown";<br />

break;<br />

}<br />

return color;<br />

' Visual Basic<br />

Function GetColor(ByVal s As Suit) As String<br />

Dim color As String = Nothing<br />

Select Case s<br />

Case Suit.Heart And Suit.Diamond<br />

color = "Red"<br />

Case Suit.Spade And Suit.Club<br />

color = "Black"<br />

Case Else<br />

color = "Unknown"<br />

End Select<br />

Return color<br />

End Function<br />

// F#<br />

type SuitTest =<br />

static member GetColor(s : Suit) : string =<br />

match s with<br />

| Suit.Heart | Suit.Diamond -> "Red"<br />

| Suit.Spade | Suit.Club -> "Black"<br />

| _ -> "Unknown"<br />

looPs<br />

With loops, code is executed repeatedly until a condition is met. Loops with <strong>C#</strong> are discussed in Chapter 2,<br />

“Core <strong>C#</strong> ,” including: for, while, do.while, <strong>and</strong> foreach. <strong>C#</strong> <strong>and</strong> C++/CLI are very similar with regard<br />

to looping statements; Visual Basic <strong>and</strong> F# defines different statements.<br />

for statement<br />

The for statement is similar with <strong>C#</strong> <strong>and</strong> C++/CLI. With Visual Basic, you can’t initialize a variable<br />

inside the for/to statement; you must initialize the variable beforeh<strong>and</strong>. for/to doesn’t require a Step to<br />

follow — Step 1 is the default. Just in case you don’t want to increment by 1, the Step keyword is required<br />

with for/to. F# uses the for/to/do <strong>and</strong> for/downto/do keywords:<br />

// <strong>C#</strong><br />

for (int i = 0; i < 100; i++)<br />

{<br />

Console.WriteLine(i);<br />

}<br />

// C++/CLI<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!