Step 3: If-Statements (Making Decisions)

In Step 3, we’re going to learn how to make your program decide what to do using if-statements. This lets your program run different code based on certain conditions, allowing it to be more interactive and flexible.

What is an If-Statement?

An if-statement checks whether a condition is true or false, and then it decides which block of code to run. This is useful for situations where you want the program to behave differently depending on certain values.

Basic Structure of an If-Statement

Here’s the basic structure of an if-statement:

    
if (condition)
{
    // Code to run if the condition is true
}
    
  • if: Starts the if-statement.
  • condition: A condition is something that evaluates to true or false.
  • If the condition is true, the code inside the curly braces {} will run.

Example of an If-Statement

Let’s say you want to check if someone is old enough to drive. Here’s a simple if-statement that does this:

    
int age = 16;

if (age >= 18)
{
    Console.WriteLine("You are old enough to drive!");
}
else
{
    Console.WriteLine("You are not old enough to drive.");
}
    

Explanation:

  • age >= 18: This condition checks if the age is 18 or greater.
  • If the condition is true (the person is 18 or older), the program will print “You are old enough to drive!”.
  • If the condition is false (the person is younger than 18), the program will print “You are not old enough to drive.”

Else and Else If

You can also add else and else if to your if-statement to check multiple conditions and provide different outcomes.

Else Statement

The else block will run if the if condition is false.

Example:

    
int temperature = 30;

if (temperature > 35)
{
    Console.WriteLine("It's very hot!");
}
else
{
    Console.WriteLine("The weather is normal.");
}
    

Else If Statement

If you want to check more than one condition, you can use else if:

    
int score = 85;

if (score >= 90)
{
    Console.WriteLine("You got an A!");
}
else if (score >= 80)
{
    Console.WriteLine("You got a B!");
}
else
{
    Console.WriteLine("You need to study more!");
}
    

Explanation:

  • If the score is 90 or above, the program will print “You got an A!”.
  • If the score is 80 or above (but less than 90), it will print “You got a B!”.
  • If the score is less than 80, it will print “You need to study more!”

Logical Operators

Sometimes, you want to check more than one condition at the same time. For example, you might want to check if someone is between 13 and 19 years old to see if they’re a teenager.

You can do this using logical operators like && (AND) and || (OR).

Example with AND (&&):

    
int age = 15;

if (age >= 13 && age <= 19)
{
    Console.WriteLine("You are a teenager.");
}
else
{
    Console.WriteLine("You are not a teenager.");
}
    

Explanation:

  • age >= 13 && age <= 19: This checks if both conditions are true (age is 13 or greater and age is 19 or less).
  • If both conditions are true, the program will print “You are a teenager.”
  • If either condition is false, it will print “You are not a teenager.”

Summary of Step 3:

  • If-statements allow your program to make decisions based on conditions.
  • You can use else to handle what happens if the condition is false.
  • Use else if to check multiple conditions.
  • Logical operators like && (AND) and || (OR) help you check more complex conditions.

Try it Yourself!

Here’s a simple program for you to try. It asks for the user’s age and checks if they are old enough to drive:

This program:

  • Asks the user for their age.
  • Checks if the user is old enough to drive (18 or older).
  • Prints different messages based on their age.

Leave a Reply

Your email address will not be published. Required fields are marked *