O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required