![]() | ![]() |
The “if” statement is simplest of the conditional statement. It has two possible return values: true or false. It returns true if the condition satisfies, else it returns false. The syntax of the “if” statement is simple:
if(condition == true)
// execute this
––––––––
Take a look at a very simple example of the “if” statement:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Are you a student? yes/no");
string student = Console.ReadLine();
if (student == "yes")
Console.WriteLine("You are a student");
Console.ReadKey();
}
}
}
In the script above, the user is asked to enter yes or no in response to the question that if he is a student. If the user enters yes, the “if” statement will return true and a line is printed on the console telling the user that you are a student, else the “if” statement returns false and the code inside the “if” statement does not execute, and nothing will be printed on the console. The output looks like this: