Link Search Menu Expand Document

ad


if - else statement

An if statement identifies which statement to run based on the value of a Boolean (true or false) condition.

The following are some common comparators that can be used when you set up conditions:

  • if(a == b) : if a is equal to b;
  • if(a < b) : if a is less than b;
  • if(a > b) : if a is greater than b;
  • if(a != b) : if a is not equal to b;

Type the following code :

using System;

namespace LearnCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string result = string.Empty;
            int number = 5;

            if (number == 12)
            {
                result = "dozen";
            }
            else
            {
                result = "not a dozen";
            }

            Console.WriteLine($"The amount {number} is {result}");
        }
    }
}
  • Run the application in the Console Window

Output in Console Window

The amount 5 is not a dozen

Bob Tabor Videos

Courtesy Microsoft and Bob Tabor


NOTE - These videos are a little dated, Visual Studio 2013/2015 is being used here. But the principles are basically the same.


The If Decision Statement


ad



Copyright © 2020 Ray Consulting Limited - Coding Skills.IO.