Variables and Data Types - potatoscript/csharp GitHub Wiki
π§βπ» Variables and Data Types π§βπ»
Letβs go deeper into programming by learning about Variables and Data Types. These are like the tools you need to organize your program and store the information youβll use. Think of them as boxes where you can keep different types of things like numbers, words, and more! π
π§³ What Are Variables? π§³
A variable is like a box where you can store information. Just like you have boxes to keep your toys, clothes, and books, variables keep different pieces of data in a program. Each variable has a name so you can easily find and use it later. π¦
For example, if you wanted to keep track of your age, you could store it in a box called age
. π§
π What Are Data Types? π
Data types are like the labels you put on the boxes to tell you what kind of stuff is inside. For example, if you have a box labeled "Toys", you know itβs for storing toys, not food. In programming, data types are the labels that tell us what kind of data a variable can hold.
There are many types of data, and we'll cover the most common ones:
π Common Data Types in C# π
Here are the most common data types you'll use in C#:
Data Type | What It Does | Example |
---|---|---|
int |
Stores whole numbers (no decimals) | int age = 10; |
double |
Stores numbers with decimals | double height = 1.75; |
string |
Stores words or sentences | string name = "Cody"; |
char |
Stores single characters (like a letter or symbol) | char grade = 'A'; |
bool |
Stores true or false values | bool isHappy = true; |
Each data type is used for a specific kind of information.
π Let's See the Data Types in Action! π
Letβs get our robot friend Cody to help us understand how to use variables and data types! Cody will store some of his personal details in variables.
Hereβs the full code with examples:
using System; // Tells the program we need to use the tools in the System library.
class Program // Where we put Cody's instructions
{
static void Main() // This is where Cody starts working.
{
// Let's help Cody introduce himself by storing some information in variables!
// Cody's age (whole number)
int age = 10;
Console.WriteLine("Cody's age is: " + age); // Outputs: Cody's age is: 10
// Cody's height (decimal number)
double height = 1.75;
Console.WriteLine("Cody's height is: " + height + " meters"); // Outputs: Cody's height is: 1.75 meters
// Cody's name (string of characters)
string name = "Cody";
Console.WriteLine("Cody's name is: " + name); // Outputs: Cody's name is: Cody
// Cody's grade (single character)
char grade = 'A';
Console.WriteLine("Cody's grade is: " + grade); // Outputs: Cody's grade is: A
// Cody's mood (true or false)
bool isHappy = true;
Console.WriteLine("Is Cody happy? " + isHappy); // Outputs: Is Cody happy? True
}
}
π Explaining the Code π
Let's look at what each part of the program does:
-
int age = 10;
π§βπ¦°int
is a data type that holds whole numbers.age
is the variable's name, and weβre giving it the value of 10 (Codyβs age).- The program then prints: "Cody's age is: 10" on the screen.
-
double height = 1.75;
πdouble
is a data type for numbers that have decimals.height
stores Cody's height as a decimal number.- The program prints: "Cody's height is: 1.75 meters".
-
string name = "Cody";
πstring
is a data type that stores text (like words or sentences).name
stores Cody's name, which is a word.- The program prints: "Cody's name is: Cody".
-
char grade = 'A';
πchar
is a data type for a single character. Codyβs grade is stored in this variable.- The program prints: "Cody's grade is: A".
-
bool isHappy = true;
πbool
is a data type that stores only two values: true or false. Here, we store whether Cody is happy.- The program prints: "Is Cody happy? True".
π¨ Creating Your Own Variables! π¨
Now that we understand how to store different types of data, letβs practice by creating our own variables:
-
Favorite Color:
- Create a variable called
favoriteColor
to store your favorite color. - You can use
string
because colors are words! For example:string favoriteColor = "Blue";
- Create a variable called
-
Pet's Age:
- Create a variable called
petAge
to store your pet's age. Useint
if your petβs age is a whole number.int petAge = 5;
- Create a variable called
-
Temperature:
- If you want to store the current temperature, you can use
double
because temperatures often have decimals.double temperature = 22.5; // Temperature in Celsius
- If you want to store the current temperature, you can use