C# else Statement - Complete Guide
Introduction to else Statements
The else statement in C# provides an alternative block of code when the condition in an if statement evaluates to false. It ensures that code executes for both true and false cases of a condition.
The else statement must always follow an if statement and cannot be used on its own. It is essential for handling two possible outcomes in decision-making logic.
Basic else Statement Syntax
The else statement runs only if the if condition is false:
Example
using System;
namespace ElseStatementExample
{
class Program
{
static void Main(string[] args)
{
int number = 3;
if (number > 5)
{
Console.WriteLine("Number is greater than 5");
}
else
{
Console.WriteLine("Number is 5 or less");
}
int age = 16;
if (age >= 18)
{
Console.WriteLine("You are an adult");
Console.WriteLine("You can vote");
}
else
{
Console.WriteLine("You are a minor");
Console.WriteLine("You cannot vote");
}
string name = "Bob";
if (name == "Alice")
{
Console.WriteLine("Hello, Alice!");
}
else
{
Console.WriteLine("Hello, stranger!");
}
bool isRaining = false;
if (isRaining)
{
Console.WriteLine("Bring an umbrella");
}
else
{
Console.WriteLine("No need for an umbrella");
}
int score = 75;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}
else
{
if (score >= 80)
{
Console.WriteLine("Grade: B");
}
else
{
if (score >= 70)
{
Console.WriteLine("Grade: C");
}
else
{
Console.WriteLine("Grade: F");
}
}
}
}
}
}
Output
Number is 5 or less You are a minor You cannot vote Hello, stranger! No need for an umbrella Grade: C
Practical Examples of else Statements
else statements are often used in real-world programming tasks such as validation, classification, and conditional logic:
Example
using System;
namespace PracticalElseExamples
{
class Program
{
static void Main(string[] args)
{
string username = "admin";
string password = "password123";
if (username == "admin" && password == "secret123")
{
Console.WriteLine("Login successful!");
}
else
{
Console.WriteLine("Invalid username or password");
}
int number = -5;
if (number > 0)
{
Console.WriteLine("Positive number");
}
else if (number < 0)
{
Console.WriteLine("Negative number");
}
else
{
Console.WriteLine("Zero");
}
int age = 25;
bool hasID = true;
if (age >= 21 && hasID)
{
Console.WriteLine("You can enter the club and drink alcohol");
}
else if (age >= 18 && hasID)
{
Console.WriteLine("You can enter the club but cannot drink alcohol");
}
else
{
Console.WriteLine("You cannot enter the club");
}
string filePath = "C:\\temp\\file.txt";
bool fileExists = false; // Simulated value
if (fileExists)
{
Console.WriteLine("File exists. Processing...");
}
else
{
Console.WriteLine("File does not exist. Creating new file...");
}
string userInput = "";
if (!string.IsNullOrEmpty(userInput))
{
Console.WriteLine("Processing your input: " + userInput);
}
else
{
Console.WriteLine("Please provide some input");
}
}
}
}
Output
Invalid username or password Negative number You can enter the club and drink alcohol File does not exist. Creating new file... Please provide some input