Welcome to Lesson 5! In this lesson, we’ll explore conditional statements in C#, which allow your programs to make decisions based on certain conditions. Conditional statements are essential for controlling the flow of execution in your applications. We’ll cover the if
, else if
, and else
statements, and how to use them to create logical branches in your code.
1. The if
Statement
The if
statement is used to execute a block of code only if a specified condition is true. It’s the simplest form of conditional statement.
Syntax:
if (condition)
{
// Code to execute if the condition is true
}
Example:
int temperature = 30;
if (temperature > 25)
{
Console.WriteLine("It's a hot day!");
}
Explanation:
- The condition checks if
temperature
is greater than 25. If true, it prints “It’s a hot day!” to the console.
2. The else
Statement
The else
statement follows an if
statement and is executed when the if
condition is false. It provides an alternative block of code to execute when the initial condition isn’t met.
Syntax:
if (condition)
{
// Code to execute if the condition is true
}
else
{
// Code to execute if the condition is false
}
Example:
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are an adult.");
}
else
{
Console.WriteLine("You are a minor.");
}
Explanation:
- The condition checks if
age
is 18 or older. If true, it prints “You are an adult.” Otherwise, it prints “You are a minor.”
3. The else if
Statement
The else if
statement allows you to check multiple conditions sequentially. It’s used when you need to evaluate more than two 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 score = 85;
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: D or below");
}
Explanation:
- The program checks the
score
and prints the appropriate grade based on the value. If none of the conditions match, it falls through to theelse
block.
4. Nested Conditional Statements
You can nest if
statements within other if
statements to handle more complex logic.
Example:
int number = 45;
if (number > 0)
{
if (number % 2 == 0)
{
Console.WriteLine("The number is positive and even.");
}
else
{
Console.WriteLine("The number is positive and odd.");
}
}
else
{
Console.WriteLine("The number is zero or negative.");
}
Explanation:
- The outer
if
checks ifnumber
is positive. If true, it further checks if the number is even or odd with an innerif
.
5. Combining Conditions
You can combine multiple conditions using logical operators such as &&
(AND) and ||
(OR) in your if
statements.
Example:
int age = 25;
bool hasLicense = true;
if (age >= 18 && hasLicense)
{
Console.WriteLine("You are eligible to drive.");
}
else
{
Console.WriteLine("You are not eligible to drive.");
}
Explanation:
- The condition checks if both
age
is 18 or older andhasLicense
is true. If both conditions are met, it prints “You are eligible to drive.” Otherwise, it prints “You are not eligible to drive.”
6. Example: Putting It All Together
Here’s a comprehensive example demonstrating the use of if
, else if
, and else
statements:
Code Example:
using System;
class Program
{
static void Main()
{
int score = 78;
bool hasPassed = true;
if (score >= 90)
{
Console.WriteLine("Excellent!");
}
else if (score >= 80)
{
Console.WriteLine("Good job!");
}
else if (score >= 70)
{
Console.WriteLine("Well done!");
}
else
{
Console.WriteLine("Needs improvement.");
}
if (hasPassed)
{
Console.WriteLine("Congratulations on passing!");
}
else
{
Console.WriteLine("Sorry, you did not pass.");
}
}
}
Explanation:
- The first set of
if
,else if
, andelse
statements evaluates thescore
and provides feedback. - The second
if
statement checks if thehasPassed
condition is true and prints an appropriate message.
Summary
In this lesson, you learned how to use if
, else if
, and else
statements in C# to make decisions in your code. Conditional statements are powerful tools for controlling the flow of execution based on various conditions. We also explored nesting and combining conditions to handle more complex scenarios.
Happy coding!
0 Comments