Welcome back to The Coding College! In this tutorial, we will learn about C# If…Else statements, which help in decision-making in your programs.
By the end of this guide, you will understand:
- The concept of conditional statements.
- How to use
if
,else
, andelse if
in C#. - Practical examples of decision-making in C#.
For more tutorials on coding and programming, visit The Coding College.
What are If…Else Statements?
Conditional statements like if
, else
, and else if
allow you to execute different blocks of code based on specific conditions.
if
statement: Executes a block of code if a condition is true.else
statement: Executes a block of code if the condition inif
is false.else if
statement: Checks multiple conditions sequentially.
C# If Statement
The if
statement checks a condition and runs the code block if the condition is true.
Syntax
if (condition)
{
// Code to execute if condition is true
}
Example: Simple If Statement
using System;
class Program
{
static void Main()
{
int age = 20;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote.");
}
}
}
Output:
You are eligible to vote.
C# If…Else Statement
The if...else
statement allows you to define an alternative code block to run if the condition is false.
Syntax
if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}
Example: If…Else Statement
using System;
class Program
{
static void Main()
{
int age = 16;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
}
}
Output:
You are not eligible to vote.
C# If…Else If…Else Statement
The else if
statement allows you to check multiple conditions sequentially. If one condition is true, the corresponding block of code will run.
Syntax
if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition2 is true
}
else
{
// Code to execute if all conditions are false
}
Example: Else If Statement
using System;
class Program
{
static void Main()
{
int marks = 85;
if (marks >= 90)
{
Console.WriteLine("Grade: A+");
}
else if (marks >= 75)
{
Console.WriteLine("Grade: A");
}
else if (marks >= 60)
{
Console.WriteLine("Grade: B");
}
else
{
Console.WriteLine("Grade: C");
}
}
}
Output:
Grade: A
Nested If Statements
You can also use if statements inside other if statements. This is known as nested if.
Example: Nested If Statements
using System;
class Program
{
static void Main()
{
int number = 25;
if (number > 0)
{
Console.WriteLine("The number is positive.");
if (number % 2 == 0)
{
Console.WriteLine("The number is even.");
}
else
{
Console.WriteLine("The number is odd.");
}
}
else
{
Console.WriteLine("The number is not positive.");
}
}
}
Output:
The number is positive.
The number is odd.
Logical Operators in If Statements
You can use logical operators to combine multiple conditions in an if
statement.
Operator | Description | Example |
---|---|---|
&& | Logical AND | age >= 18 && age < 60 |
` | ` | |
! | Logical NOT | !(age > 18) |
Example: Using Logical Operators
using System;
class Program
{
static void Main()
{
int age = 30;
if (age >= 18 && age <= 60)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are either too young or too old.");
}
}
}
Output:
You are an adult.
Why If…Else is Important
- Decision-Making: It allows programs to make decisions based on conditions.
- Flexibility: Supports multiple conditions with
else if
andelse
. - Efficiency: Helps control program flow, making code efficient and dynamic.
Key Points to Remember
- Use
if
to test a condition. - Use
else
to handle when the condition is false. - Use
else if
to test multiple conditions sequentially. - Combine conditions using logical operators (
&&
,||
,!
). - Conditions are evaluated top to bottom, so order matters.
Conclusion
The if…else statement is a powerful tool for decision-making in C#. By understanding its syntax and use cases, you can write dynamic and efficient programs. Practice these examples and experiment with your own conditions.
For more C# tutorials and coding resources, visit The Coding College. Keep coding and keep learning! 🚀