If Else Statements - potatoscript/csharp GitHub Wiki
๐ If-Else Statements in C# ๐
๐งโ๐ป What Are If-Else Statements? ๐งโ๐ป
Imagine you are deciding whether to go outside to play based on the weather. If itโs sunny, youโll go outside. If itโs raining, youโll stay inside. You are making a decision based on the condition of the weather.
In programming, If-Else statements work the same way! They let you make decisions in your program by checking if something is true or false.
Hereโs the basic idea:
- If something is true, do one thing.
- Else (otherwise), do something else.
๐ท๏ธ How Do If-Else Statements Work? ๐ท๏ธ
An If-Else statement checks a condition. If the condition is true, it runs the first block of code (the If part). If the condition is false, it runs the second block of code (the Else part).
๐งโ๐ป Basic Syntax:
if (condition)
{
// Code to run if condition is true
}
else
{
// Code to run if condition is false
}
- Condition: This is the thing you're checking, like "Is the weather sunny?" or "Is my age greater than 10?"
- True block: This runs if the condition is true.
- False block: This runs if the condition is false.
๐งโ๐ป Letโs Make a Fun Example: "Should I Go Outside?" ๐งโ๐ป
Letโs say you have a program that checks if itโs sunny or raining to decide whether to go outside. Hereโs how you can write it using an If-Else statement:
using System;
class Program
{
static void Main()
{
// The current weather is sunny
string weather = "sunny"; // You can change this to "rainy"
// If-Else statement to decide whether to go outside
if (weather == "sunny")
{
Console.WriteLine("Yay! It's sunny! Let's go outside to play!");
}
else
{
Console.WriteLine("Oh no, it's raining! Let's stay inside.");
}
}
}
Explanation:
- The program checks if the weather is sunny.
- If it is sunny (
weather == "sunny"
), it prints "Yay! It's sunny! Let's go outside to play!". - If it is not sunny (i.e., itโs raining or something else), it prints "Oh no, it's raining! Let's stay inside."
๐งโ๐ป Letโs Try Another Example: "How Old Are You?" ๐งโ๐ป
Now letโs make a program that checks your age and tells you if you are allowed to enter a party. If you're older than 18, you can enter; otherwise, you can't.
using System;
class Program
{
static void Main()
{
// The user's age
int age = 15; // Change this to test with different ages
// If-Else statement to check age
if (age >= 18)
{
Console.WriteLine("You are allowed to enter the party! ๐");
}
else
{
Console.WriteLine("Sorry, you are too young to enter the party. ๐");
}
}
}
Explanation:
- The program checks if your age is 18 or older (
age >= 18
). - If you are 18 or older, it prints "You are allowed to enter the party!".
- If you are younger than 18, it prints "Sorry, you are too young to enter the party."
๐งโ๐ป Multiple If-Else Statements: More Than One Condition ๐งโ๐ป
What if you want to check multiple conditions? You can use If-Else-If to check more than one condition.
๐งโ๐ป Example: "What Time Is It?"
Let's create a program that checks what time of day it is and tells you what you should be doing:
using System;
class Program
{
static void Main()
{
// The current time of day (24-hour format)
int time = 14; // Change this to test with different times
// If-Else-If statements to check the time
if (time >= 6 && time < 12) // Morning (6 AM to 12 PM)
{
Console.WriteLine("Good morning! Time for breakfast! ๐ฅ");
}
else if (time >= 12 && time < 17) // Afternoon (12 PM to 5 PM)
{
Console.WriteLine("Good afternoon! Time for lunch! ๐");
}
else if (time >= 17 && time < 21) // Evening (5 PM to 9 PM)
{
Console.WriteLine("Good evening! Time for dinner! ๐ฝ๏ธ");
}
else // Night (9 PM to 6 AM)
{
Console.WriteLine("Good night! Time to sleep! ๐");
}
}
}
Explanation:
- This program checks the time and decides if itโs morning, afternoon, evening, or night.
- The program will print a message based on what time of day it is.
- The If-Else-If checks multiple conditions:
- If the time is between 6 AM and 12 PM, it prints "Good morning! Time for breakfast!".
- If itโs between 12 PM and 5 PM, it prints "Good afternoon! Time for lunch!".
- If itโs between 5 PM and 9 PM, it prints "Good evening! Time for dinner!".
- If itโs night (9 PM to 6 AM), it prints "Good night! Time to sleep!".
๐ก Things to Remember ๐ก
- If-Else statements help you make decisions in your program by checking a condition.
- Use
==
to check if two things are equal. For example,if (weather == "sunny")
. - You can check multiple conditions with
else if
. - The else part is optional, but itโs helpful when you want something to happen when all conditions are false.
- Conditions can involve numbers, strings, or even more complicated logic.