if statement in C#

Shahzad Aslam
5 min readSep 7, 2024

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.

if statement in C#

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.

flow-diagram of 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);
}
}
}

Output

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet