Tag Archives: statement

Switch statements in C#

This tutorial explains how to write switch statements in C# language. Switch statements are used to select one from many different blocks of code. A switch statement can take a variable and check if its value matches one of many different specified values. Once the switch statement finds a matching case, it will run the code inside that case and the stop testing all the other cases.

Watch the video below and then scroll down to the sample code to see how switch statements work.

Sample code

using System;

namespace MyCSharpProject
{
  class Program
  {
    static void Main(string[] args)
    {
      int myNumber = 5;
      switch (myNumber) {
        case 1:
          Console.WriteLine("The number is 1");
          break;
        case 2:
          Console.WriteLine("The number is 2");
          break;
        case 3:
          Console.WriteLine("The number is 3");
          break;
        default:
          Console.WriteLine("I don't know this number");
          break;
      }
    }
  }
}

Next tutorial: While loops in C#

Switch statements in C#

We have looked at how to use if statements for conditional programming in C#. However, there is something else we can use if there are many different conditions or cases we want to test. We can use switch statements. Switch statements can contain a number of ‘cases’ and a ‘default’ if none of the specified cases are chosen or met.

Have a look at the sample code below. In this program, the user is asked to enter a number between 1 and 5. Based on the number the user enters, they will be presented with an interesting fact about that number. Each number is a different case and there is also ‘default’ case that will be used if the user does not enter one of those 5 numbers.

Each case also uses a ‘break’ so that if a case is met, then the other cases will not also be checked. In other words, the switch statement will end as soon as a case is met. Here is the sample code:

using System;

namespace MyCSharpApp
{
  class MainClass
  {
    public static void Main (string[] args)
    {
      // Ask the user to enter a number between 1 and 5
      Console.WriteLine ("Enter a number between 1 and 5 for an interesting fact:");
      int number = int.Parse( Console.ReadLine ());

      switch (number) {
      case 1:
        Console.WriteLine ("1 is the number of websites that existed in 1991.");
        break;
      case 2:
        Console.WriteLine ("2 is the only even prime number.");
        break;
      case 3:
        Console.WriteLine ("3 is the number of days you can survive without water (on average).");
        break;
      case 4:
        Console.WriteLine ("4 is the number of miles per second that space junk moves around Earth.");
        break;
      case 5:
        Console.WriteLine ("5 is the length in meters of a Great White Shark.");
        break;
      default:
        Console.WriteLine ("You did not select a valid option.");
        break;
      }

      Console.ReadLine ();
    }
  }
}

Here is an example of what the user will see when they enter the number 4…

switch_cs_example

Note: When copying the code, only copy the actual code you need. The code above uses a namespace that may be different to the name of your project or solution. If you copy all of the code above and replace all of your code with it, then make sure you change the namespace name above to match the one in your project.

Echo statement and comments

This tutorial introduces the echo statement for displaying messages and also discusses comments that can be added to your code. Echo statements can be used to output simple messages, variable values, and also HTML code. You can even include JavaScript code within echo statements. The echo statement is similar to the print statement you might see in many other languages.

For example, the statement echo “Hello world”; will display the message “Hello world”. The statement echo $myVariable; will output the value of the variable called $myVariable. The statement echo “<h1>My heading</h1>”; will output text formatted in a h1 size heading (using the h1 heading HTML tag).

You could even add JavaScript code to an echo statement, for example: echo ‘<script>alert(“Hello world”);</script>’;

Watch the video below and then scroll down to see the sample code:

PHP sample code:

<?php
 // this is a single-line comment
 
 /* 
 this
 is
 a
 multi-line
 comment
 */
 
 ############################
 # this is a comment block #
 ############################

 echo "Hello world"; // this is a string in an echo statement
 echo "He said \"Hello world\""; // this is an example of escaping quotation marks
 echo 'Hello world'; // this is an example of a string inside single quotation marks
?>

A reminder about the ?> tag

If you are a beginner PHP programmer, it is recommended that you omit the closing ?> PHP tag at the end of your code if the file is pure PHP code. This will prevent any unwanted effects that may occur if you add whitespace after the closing tag.

If you do use the closing ?> then it is important that you do not add any other white space (spaces or new lines) after the tag. You can read the reasons why at the official PHP documentation site.

PHP Manual references:

If statements

An ‘if statement’ is used to test a condition. If the test condition evaluates to true (Eg. x is less than y), then the code inside the if statement will run. If the test condition evaluates to false, then the code inside the if statement won’t run and the program will continue on. Take a look at the video here which explains if statements, and then try using the sample code below.

<!DOCTYPE html>
<html lang="en"
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - If Statements</title>
 <script type="text/javascript">
 
 /* For an 'if' statement, the syntax should look like:
 
 if(condition to be tested){
 //run code here
 }
 
 */
 
 var age = 20 // declare the variable age
 
 if(age>=18){
 document.write("You are old enough to vote."); // this message only displays if age is greater than or equal to 18
 }
 
 /* Operators you can use include:
 == equal to
 != not equal to
 > greater than
 < less than
 >= greater than or equal to
 <= less than or equal to
 */
 
 </script>
 
</head>
<body>
 
</body>
</html>

Nested ‘If’ Statements in JavaScript

This tutorial explains how to create nested IF statements in JavaScript. A nested IF statement is an IF statement that is inside another IF statement. Also known as…ifception 😉 Using nested IF statements is not always the best way to code but it is handy to know them anyway.

The sample code below shows how to use if statements inside other if statements (nested if statements).

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Nesting If Statements</title>
 <script type="text/javascript">
 
 var age = 21;
 var maxAge = 30;
 var minAge = 18;
 
 // Below is an example of using if statements inside other if statements
 // This is called nested if statements.
 
 if(age>=minAge){
 if(age<=maxAge){
 document.write("You are within the accepted age range.");
 }
 else{
 document.write("You are above the accepted maximum age.");
 }
 }
 else{
 document.write("You are under the accepted minimum age.");
 }
 
 </script>
 
</head>
<body>
 
</body>
</html>

Next tutorial: Using and/or operators in a complex If statement