About This eBook by Bart De Smet

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

O'Reilly logo

The if Statement

Starting by evaluating a Boolean expression, the if statement can conditionally execute an embedded statement. Two forms exist, depending on whether you want to handle the negative case:

// Ignore when condition evaluates false.if (condition)    statement-if-true// Also handle the false case.if (condition)    statement-if-trueelse    statement-if-false

Let’s look at an example that uses both branches:

static void Main(){    var rand = new Random();    int n = rand.Next(0, 100);    PrintEvenOrNot(n);}static void PrintEvenOrNot(int n){    if (n % 2 == 0)    {        Console.WriteLine("Even");    }    else    {        Console.WriteLine("Odd");    }}

In the Main method, ...

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