Chapter 11. Iteration and flow control


This chapter deals with the basic issue of iteration (looping) and flow control in C#.

Like Java

  • In C#, the while (in while loops ), do and while (in do...while loops), and for (in for loops) keywords work in exactly the same way as in Java.

  • You can break and continue from within loops to terminate iteration prematurely (except that you cannot use them with labels).

Unlike Java

Flow control in C# is very much the same as Java, except for:

  • a new and useful foreach keyword;

  • some changes to how switch-case is used;

  • C# retains the goto keyword; [1]

    [1] goto is a reserved word in Java, but has no functionality. The usage of goto in C/C++ has often been criticized as poor structural programming. Use it with care in C#.

  • you cannot use break and continue with labels.

Short examples are given showing while / do...while / for loops and the use of continue and break , but they will not be described in detail because they are used the same way in Java. The later sections emphasize the new stuff that came with C#.



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