11.1 Looping with the while , do , for , continue and break keywords


11.1 Looping with the while , do , for , continue and break keywords

The use of these keywords in looping is identical in Java and C# (with some exceptions with the break and continue keywords). There follows some examples for completeness. Feel free to skip this section if you are already familiar using looping with these keywords in Java.

11.1.1 Use of while in loops

Here is an example of a while loop in C#.

 30:  public static void PrintNumbersUsingWhile(){ 31:    int i=0; 32:  while (i<10)  33:      Console.WriteLine(i++); 34:  } 

PrintNumbersUsingWhile() prints out the numbers 0 to 9.

Here is another example.

 30:  public static void GetUserInputUsingWhile(){ 31:    Console.WriteLine("You must enter Y to return"); 32:    string UserInput = Console.ReadLine(); //read keyboard 33:  while (true){  34:      if (UserInput.Equals("Y")) 35:  break;  36:      Console.WriteLine("You must enter Y to return"); 37:      UserInput = Console.ReadLine(); 38:    } 39:    return; 40:  } 

GetUserInputUsingWhile() reads a string from the keyboard (line 32), and goes into a infinite while loop (line 33), breaking out only when the string the user enters is a capital 'Y'.

Like Java
  • You can break out of a while loop using the break keyword.

  • You can bypass the remaining statements in a loop and go directly to the conditional using the continue keyword.

  • while takes in only a boolean value, or an expression which evaluates to a boolean type only. [2]

    [2] while in C/C++ takes in both booleans and integers. Zero is considered 'false' and any other integer “ regardless of sign “ is considered 'true'. Lots of programming bugs have been attributed to this 'loose typing' feature of C/C++. We have the classic mistake of accidentally leaving out one equal character in while(intX == intY) to become while(intX = intY) . This compiles in C/C++ and during runtime the value stored in intY is assigned to intX , and if intX is a non-zero integer the while condition is fulfilled. Such bugs are extremely difficult to detect, but often deadly . Fortunately, the while construct is extremely strict in this aspect for both Java and C# “ we would prefer a compilation error than difficult-to-find logic errors.

Unlike Java

You cannot use the break and continue keywords together with labels (see section 11.5).

11.1.2 Use of do and while in loops

Here is an example of a do ... while loop in C#.

 30:  public static void GetUserInputUsingDoWhile(){ 31:    string UserInput; 31:  do {  35:      Console.WriteLine("You must enter Y to return"); 32:      UserInput = Console.ReadLine(); 36:  }   while (!UserInput.Equals("Y"));  38:    return; 39:  } 

GetUserInputUsingDoWhile() is functionally identical to GetUserInput UsingWhile() in the previous section, except that the former uses do...while instead of just a while construct.

11.1.3 Use of for in loops

Here is an example of a for loop in C#.

 30:  public static void PrintNumbersUsingFor(){ 31:   for (int i=0; i<10; i++) 32:      Console.WriteLine(i++); 33:  } 

PrintNumbersUsingFor() prints out the numbers 0 to 9.

Like Java
  • The initialization expression, loop condition expression, and increment expression [3] are all optional. The following is perfectly valid in both C# and Java, and results in a infinite loop:

    [3] for syntax: for(initialization expression; loop condition expression; increment expression){...}

     // similar to while (true) {} for (;;) {   // code here } 


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