if statement in C#
The if statement is a fundamental control structure in programming that allows you to make decisions and execute different blocks of code based on certain conditions.
Here, in below example
1. int price = 20; — We have declared and initialized a local varaible with named int price = 20. We will use this price variable to test boolean expression in if-condition.
2. if (price == 20) — we have if (price == 20) a boolean expression which will be true or false. It will test if price is equal 20 then will return true otherwise return false.
3. Console.WriteLine(“Apple Price is {0}.”, price); — When If condition is true then block of statement will execute and it will print “Apple Price is 20”. When if condition will be false then block of if-condition below of if-condition will be skipped and controls will go outside the if condition.
if statement with example
using System;
class CSharpProgram
{
static void Main(string[] arge)
{
int price = 20;
if (price == 20)
{
Console.WriteLine("Apple Price is {0}.", price);
}
}
}