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 ๐Ÿ’ก

  1. If-Else statements help you make decisions in your program by checking a condition.
  2. Use == to check if two things are equal. For example, if (weather == "sunny").
  3. You can check multiple conditions with else if.
  4. The else part is optional, but itโ€™s helpful when you want something to happen when all conditions are false.
  5. Conditions can involve numbers, strings, or even more complicated logic.