Statements and Execution Flow

C# provides many statements similar to those found in C, C++, and Java. The statement types discussed in this appendix are selection statements, looping statements, and jump statements.

Selection Statements

Selection statements control the flow of execution based on an expression's value. C# supports two types of selection statements: if and switch.

if

The if statement controls program execution based on the outcome of a Boolean expression. Listing A.9 shows a simple example of an if statement in C#.

Listing A.9. The if Statement
using System;

class MyClass{

   public static void Main(){

      int x = 1;
      int y = 2;

      if(x == y){
         Console.WriteLine("x has the same value as y.");
      }
   }
}

The else statement is used with if to ...

Get Building e-Commerce Sites with the .NET Framework 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.