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(); ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.