C# Syntax

C# Syntax

In the previous chapter, we created a C# file called Program.cs, and we used the following code to print “Hello World” to the screen:

Program.cs

using System;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Hello World!");    
    }
  }
}

Result:

Hello World!

 

Continue reading C# Syntax

C++ Switch

C++ Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax

switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works:

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break and default keywords are optional, and will be described later in this chapter

The example below uses the weekday number to calculate the weekday name: Continue reading C++ Switch

C Switch

Switch Statement

Instead of writing many if..else statements, you can use the switch statement.

The switch statement selects one of many code blocks to be executed:

Syntax

switch (expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

This is how it works :

  • The switch expression is evaluated once
  • The value of the expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The break statement breaks out of the switch block and stops the execution
  • The default statement is optional, and specifies some code to run if there is no case match

Continue reading C Switch