Flow Control

After this first introduction to C#, we'll examine flow control and control structures. We'll need this information to implement code that is executed only under certain circumstances.

If/Else

Conditional execution is a core component of every programming language. Just like C and C++, C# supports If statements. To see how If statements work, we've implemented a trivial example:

using System;

class   Hello
{
        public static void Main()
        {
                int     number = 22;
                if      (number > 20)
                        Console.WriteLine("if branch ...");
                else
                {
                        Console.WriteLine("else branch ...");

                }
        }
}

Inside the C# program, we define an integer value. After that the system checks whether the value is higher than 20. If the condition is true, the code inside the If block is executed. ...

Get Mono Kick Start now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.