C# if Statement - Complete Guide
Introduction to if Statements
The if statement in C# is a fundamental control flow structure that allows you to execute code conditionally based on whether a Boolean expression evaluates to true. It forms the basis of decision-making in C# programs.
if statements enable your programs to respond differently to different conditions, making them more dynamic and intelligent. Understanding how to use if statements effectively is crucial for writing logical and responsive code.
Basic if Statement Syntax
The basic syntax of an if statement in C# is straightforward:
Example
using System;
class Program
{
static void Main()
{
int number = 10;
if (number > 5)
{
Console.WriteLine("The number is greater than 5");
}
int age = 20;
if (age >= 18)
{
Console.WriteLine("You are an adult");
Console.WriteLine("You can vote");
Console.WriteLine("You can drive");
}
int temperature = 25;
if (temperature > 30)
Console.WriteLine("It's hot outside");
Console.WriteLine("This line always executes");
string name = "Alice";
if (name == "Alice")
{
Console.WriteLine("Hello, Alice!");
}
bool isRaining = true;
if (isRaining)
{
Console.WriteLine("Remember to bring an umbrella!");
}
}
}
Output
The number is greater than 5 You are an adult You can vote You can drive This line always executes Hello, Alice! Remember to bring an umbrella!
Comparison Operators in if Statements
if statements often use comparison operators to evaluate conditions:
Example
using System;
class Program
{
static void Main()
{
int x = 10;
int y = 5;
if (x == 10) Console.WriteLine("x is equal to 10");
if (x != y) Console.WriteLine("x is not equal to y");
if (x > y) Console.WriteLine("x is greater than y");
if (y < x) Console.WriteLine("y is less than x");
if (x >= 10) Console.WriteLine("x is greater than or equal to 10");
if (y <= 5) Console.WriteLine("y is less than or equal to 5");
string name1 = "Alice";
string name2 = "alice";
if (name1 == "Alice") Console.WriteLine("Name is Alice");
if (name1 != name2) Console.WriteLine("Names are different (case-sensitive)");
if (name1.Equals(name2, StringComparison.OrdinalIgnoreCase))
Console.WriteLine("Names are the same (case-insensitive)");
}
}
Output
x is equal to 10 x is not equal to y x is greater than y y is less than x x is greater than or equal to 10 y is less than or equal to 5 Name is Alice Names are different (case-sensitive) Names are the same (case-insensitive)
Logical Operators in if Statements
Logical operators allow you to combine multiple conditions in if statements:
Example
using System;
class Program
{
static void Main()
{
int age = 25;
bool hasLicense = true;
bool hasCar = false;
if (age >= 18 && hasLicense)
Console.WriteLine("You can drive legally");
if (hasLicense || hasCar)
Console.WriteLine("You have either a license or a car");
if (!hasCar)
Console.WriteLine("You don't have a car");
int score = 85;
bool attendedClass = true;
if (score >= 60 && attendedClass)
Console.WriteLine("You passed the course");
int temperature = 22;
bool isSunny = true;
bool isWeekend = false;
if ((temperature > 20 && isSunny) || isWeekend)
Console.WriteLine("Good day for outdoor activities");
if (age >= 18)
{
if (hasLicense)
{
if (hasCar)
Console.WriteLine("You can drive your car");
else
Console.WriteLine("You can drive but you need a car");
}
}
}
}
Output
You can drive legally You have either a license or a car You don't have a car You passed the course Good day for outdoor activities You can drive but you need a car