Conditional statements are fundamental in programming as they allow you to execute different blocks of code based on certain conditions. In C#, the primary conditional statements are if, else if, else, and switch.
- The
if Statement
if StatementThe if statement evaluates a boolean expression and executes the block of code inside it if the expression is true.
Syntax
Example
Explanation
number > 5is the condition.- If the condition is true, the message "The number is greater than 5." is printed to the console.
- The
else Statement
else StatementThe else statement follows an if statement and executes a block of code if the if condition is false.
Syntax
if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}Example
int number = 3;
if (number > 5)
{
Console.WriteLine("The number is greater than 5.");
}
else
{
Console.WriteLine("The number is not greater than 5.");
}Explanation
- If
number > 5is false, the message "The number is not greater than 5." is printed to the console.
- The
else if Statement
else if StatementThe else if statement allows you to check multiple conditions.
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 none of the above conditions are true
}Example
int number = 7;
if (number > 10)
{
Console.WriteLine("The number is greater than 10.");
}
else if (number > 5)
{
Console.WriteLine("The number is greater than 5 but less than or equal to 10.");
}
else
{
Console.WriteLine("The number is 5 or less.");
}Explanation
- The first condition
number > 10is false. - The second condition
number > 5is true, so the message "The number is greater than 5 but less than or equal to 10." is printed to the console.
- The
switch Statement
switch StatementThe switch statement is used to select one of many code blocks to be executed.
Syntax
switch (variable)
{
case value1:
// Code to execute if variable equals value1
break;
case value2:
// Code to execute if variable equals value2
break;
default:
// Code to execute if variable does not match any case
break;
}Example
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}Explanation
- The variable
dayis compared against each case. - Since
dayis 3, the message "Wednesday" is printed to the console.
Practical Exercises
Exercise 1
Write a program that checks if a number is positive, negative, or zero.
Solution
int number = -5;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}Exercise 2
Write a program that assigns a grade based on a score using a switch statement. The score ranges from 0 to 100.
Solution
int score = 85;
switch (score / 10)
{
case 10:
case 9:
Console.WriteLine("Grade: A");
break;
case 8:
Console.WriteLine("Grade: B");
break;
case 7:
Console.WriteLine("Grade: C");
break;
case 6:
Console.WriteLine("Grade: D");
break;
default:
Console.WriteLine("Grade: F");
break;
}Common Mistakes
- Forgetting the
breakstatement in aswitchcase: This can cause the program to execute the next case unintentionally. - Not using curly braces
{}inifstatements: This can lead to logical errors, especially when adding more lines of code inside theifblock.
Conclusion
In this section, you learned about the different types of conditional statements in C#. These include if, else if, else, and switch statements. Understanding and using these statements effectively allows you to control the flow of your program based on various conditions. In the next section, we will explore loops, which allow you to execute a block of code multiple times.
C# Programming Course
Module 1: Introduction to C#
- Introduction to C#
- Setting Up the Development Environment
- Hello World Program
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Control Structures
Module 3: Object-Oriented Programming
- Classes and Objects
- Methods
- Constructors and Destructors
- Inheritance
- Polymorphism
- Encapsulation
- Abstraction
Module 4: Advanced C# Concepts
- Interfaces
- Delegates and Events
- Generics
- Collections
- LINQ (Language Integrated Query)
- Asynchronous Programming
Module 5: Working with Data
Module 6: Advanced Topics
- Reflection
- Attributes
- Dynamic Programming
- Memory Management and Garbage Collection
- Multithreading and Parallel Programming
