Summary of Step 2 and Step 3: C# Basics
Step 2: Variables and Data Types
In Step 2, you learned about variables and data types, which allow you to store and manage information in your C# programs.
Variables: Containers that store information (like a name or a number). You can later use or modify this information in your program.
- Example:
string playerName = "Alice";
int playerAge = 12;
- In this example,
playerName
stores the text “Alice”, andplayerAge
stores the number 12.
- In this example,
Data Types: Specify the kind of data a variable can hold. Some common data types:
string
: Used for text (e.g.,"Alice"
).int
: Used for whole numbers (e.g.,12
).bool
: Used for true/false values (e.g.,true
orfalse
).double
: Used for decimal numbers (e.g.,3.14
).
You also learned how to display these variables with Console.WriteLine()
, like:
Console.WriteLine("Player Name: " + playerName);
Console.WriteLine("Player Age: " + playerAge);
Step 3: If-Statements (Making Decisions)
In Step 3, you learned about if-statements, which allow your program to make decisions based on certain conditions.
If-Statement: This checks if a condition is true or false, and then executes different code based on the result.
- Example:
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are old enough to drive!");
}
else
{
Console.WriteLine("You are not old enough to drive.");
}
- This code checks if the value of
age
is 18 or more. If it’s true, it prints a message saying you’re old enough to drive; if not, it prints a different message.
- This code checks if the value of
Else If: Used when you want to check multiple conditions.
Example:
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.");
}
Logical Operators: You can combine multiple conditions using logical operators like &&
(AND) and ||
(OR).
- Example:
if (age >= 13 && age <= 19)
{
Console.WriteLine("You are a teenager.");
}
Key Takeaways:
- Step 2 taught you how to store and display information using variables and different data types.
- Step 3 introduced you to if-statements, which allow your program to make decisions based on certain conditions, making your programs more interactive and flexible.
Let me know when you’re ready for the next step!