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:

  1. 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.
  2. 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".
  3. 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".
  4. 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".
  5. 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:

  1. 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";
      
  2. Pet's Age:

    • Create a variable called petAge to store your pet's age. Use int if your pet’s age is a whole number.
      int petAge = 5;
      
  3. 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