Back

Foundation: Values and Variables

divider

Foundation Activity

Values and Variables

An Optional First Step

This activity gives you a slower, console-based introduction to the ideas in Activity 1.3.

Use it before the Raylib activity or return to it for extra practice with variables and types.

Key Concepts

Storing Information With Variables

Declaring Types and Changing Values

Games Store Changing Information

Street Fighter II gameplay with a score, high score, health bars, timer, and player names in the display

Score, health, time, and names are values the game must remember.

Games Store Changing Information

Game Data
int score = 379601;
int timeRemaining = 65;
string playerName = "RYU";

Each value has a descriptive name and a type.

What Is a Variable?

A variable is a named place in memory that stores a value so the program can use it again later.

Declare a Variable

Demo
int score = 0;
  1. int is the type.
  2. score is the name.
  3. 0 is the starting value.

Three Useful Types

Demo
string playerName = "Avery";
int score = 0;
double accuracy = 82.5;
  • string stores text.
  • int stores whole numbers.
  • double stores numbers with decimals.

Types Are Part of the Declaration

In this course, we will write the type explicitly when a variable is first declared.

After that, the variable keeps the same type for the rest of its lifetime.

Display Stored Values

Demo
string playerName = "Avery";
int score = 0;
Console.WriteLine(playerName);
Console.WriteLine(score);
Demo
Avery
0

Naming Variables

  • Choose a name that explains the value
  • Begin with a lowercase letter
  • Capitalize each later word
  • Do not use spaces

This style is called camel case: playerName, timeRemaining.

Reassign a Variable

After a variable exists, use its name and = to store a new value. Do not write the type again.

Demo
int score = 10;
Console.WriteLine(score);
score = 25;
Console.WriteLine(score);
Demo
10
25

Update From the Current Value

Demo
int score = 40;
score = score + 10;
Console.WriteLine(score);
Demo
50

Update From the Current Value

C# evaluates the right side first:

  1. Read the current value of score.
  2. Add 10.
  3. Store the result back in score.

A Calculation Must Go Somewhere

Broken Demo
int score = 40;
score + 10;
Console.WriteLine(score);

This is not a complete C# statement. Store the result with score = score + 10;.

C# Protects the Type

Broken Demo
int score = 0;
score = "ten";
Compiler Output
error CS0029: Cannot implicitly convert type 'string' to 'int'

C# catches this mismatch before the program runs.

Today's Objectives

  • Declaring variables with explicit types
  • Choosing descriptive camel-case names
  • Displaying stored values
  • Reassigning and updating number variables

Key Terms

Variable
A named place in memory that stores a value.
Declaration
Creating a variable by giving it a type and name.
Assignment
Storing a value in a variable.
Data Type
The kind of value a variable is allowed to store.

Key Terms

Reassignment
Giving an existing variable a new value.
String
A type used for text.
Integer
A whole number; written as int in C#.
Double
A C# type used for numbers with decimals.

'F' → Fullscreen

divider

Build

Copy template-console in the workspace and name the copy fb-values-and-variables. Open itsProgram.cs file.


Task 1: Declare and Display Two Variables

  • Replace the starter code with the program below.
  • Change "Avery" to your first name, but keep the quotation marks.
  • Run the program and find both values in the terminal.
VALUES AND VARIABLES
string playerName = "Avery";
int score = 0;
Console.WriteLine(playerName);
Console.WriteLine(score);

Task 2: Add a Decimal Value

  • Add the accuracy variable and its output statement beneath your existing code.
  • Run the program and confirm that the decimal value appears.
  • Change the value while keeping it between 0.0 and 100.0.
VALUES AND VARIABLES
string playerName = "Avery";
int score = 0;
Console.WriteLine(playerName);
Console.WriteLine(score);
double accuracy = 82.5;
Console.WriteLine(accuracy);

Task 3: Update the Score

  • Add ten to the current score and display the result.
  • Add twenty-five more and display the result again.
  • Before running, predict both new values.
  • Run and compare the terminal with your prediction.
VALUES AND VARIABLES
string playerName = "Avery";
int score = 0;
Console.WriteLine(playerName);
Console.WriteLine(score);
double accuracy = 82.5;
Console.WriteLine(accuracy);
score = score + 10;
Console.WriteLine(score);
score = score + 25;
Console.WriteLine(score);

Task 4: Trace the Program

Make a small table on paper with two columns: statement and score after the statement. Trace each line that declares, changes, or displays score.


Challenge (Optional): Break and Repair the Type

  1. Temporarily add score = "ten"; after the score declaration.
  2. Run and locate the two conflicting types in the error message.
  3. Delete the broken statement and confirm the program works again.
divider

Checkpoint

If you kept the example values, your output should match this result. A customized name or accuracy value may differ.

Example Output
Avery
0
82.5
10
35
  • Every variable is declared before it is used.
  • The three declarations use string, int, and double.
  • The score reaches 35 after both updates.
  • The program compiles without an error.
divider

Reflection

Answer the following questions after checking your program.

  1. Explain the type, name, and starting value in int score = 0;.
  2. If score holds 10, explain how C# evaluates score = score + 25;.
  3. Why can an int variable store 10 but not "ten"?
divider

Submit

Keep your working project. This Foundation activity has no separate submission unless your instructor asks to see it.

Foundation Activity Complete