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 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.