Flow of Control Constructs

C# supports the same flow of control statements that you are familiar with from C and C++. The structure of an if statement is almost exactly the same as C and C++. C# also supports switch statements and adds the very useful ability to switch on a string. There are for, do, and while loops and a new loop called the foreach loop that you will probably find yourself using more than any other type of loop.

Let's start by looking at the if statement. The following code shows an example of a simple if statement in C#:

int x=0, y=1;
if (x > y)
    System.Console.WriteLine("x is greater than y");

You can also use else if and else clauses as follows:

 int x=0, y=1; if (x > y) System.Console.WriteLine("x is greater than y"); else ...

Get .NET and COM Interoperability Handbook, The 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.