C# Tutorial

C# IF, Switch, For, While Loop Statements Tutorial [Examples]

If statement

The if statement is used to evaluate a boolean expression before executing a set of statements. If an expression evaluates to true, then it will run one set of statements else it will run another set of statements.
In our example below, a comparison is made for a variable called value. If the value of the variable is less than 10, then it will run one statement, or else it will run on another statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
 class Program 
 {
  static void Main(string[] args) 
  {
   Int32 value = 11;
   
   if(value<10)
   {
    Console.WriteLine("Value is less than 10");
   }
   else
   {
    Console.WriteLine("Value is greater than 10");
   }
    Console.ReadKey();
  }
 }
}

Code Explanation

  1. We first define a variable called value and set it to the value of 11.
  2. We then use the 'if' statement to check if the value is less than 10 of the variable. The result will either be true or false.
  3. If the if condition evaluates to true, we then send the message "Value is less than 10" to the console.
  4. If the if condition evaluates to false, we then send the message "Value is greater than 10" to the console.
If the above code is entered properly and the program is executed successfully, the following output will be displayed.

No comments:

Post a Comment

What is ArrayList in C#?

What is ArrayList in C#? The ArrayList collection is similar to the Arrays data type in C#. The biggest difference is the dynamic natur...