Lesson 2 of 22
Lesson Progress: 0%
A variable in C++ is a named container that stores data. You can think of it as a labeled box that holds a value. Every variable has a type, which tells C++ what kind of data it can hold. Common types include int for whole numbers and string for text. You give a variable a name and a value when you declare it. The value can be changed later if needed. Variables let you store, reuse, and manipulate data throughout your program. This lesson will show you how to create and use variables in C++.
#include <iostream>
using namespace std;
int main() {
int score = 95;
cout << score;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int score = 95; declares an integer variable named score and gives it the value 95.int tells C++ that this variable will hold a whole number.= assigns the value on the right to the variable on the left.cout << score; prints the value stored in the variable.95, which is the value of score.#include <iostream>
using namespace std;
int main() {
string name = "Alice";
int age = 12;
cout << name << " is " << age;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
string name = "Alice"; creates a text variable and sets it to Alice.int age = 12; creates a number variable and sets it to 12.cout << name << " is " << age; prints both variables together.Alice is 12.#include <iostream>
using namespace std;
int main() {
int points = 10;
points = 25;
cout << points;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int points = 10; creates the variable and sets it to 10.points = 25; changes the value to 25, replacing the old value.points, it shows the current value, which is 25.10 is gone after the reassignment.#include <iostream>
using namespace std;
int main() {
int a = 15;
int b = 4;
cout << a + b;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int a = 15; and int b = 4; create two variables.cout << a + b; adds their values and prints the result.a + b is evaluated to 19 before printing.#include <iostream>
using namespace std;
int main() {
int length = 8;
int width = 5;
cout << length * width;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
int length = 8; and int width = 5; store the dimensions.cout << length * width; multiplies them to find the area.length * width gives 40.#include <iostream>
using namespace std;
int main() {
string city = "Paris";
string country = "France";
cout << city << ", " << country;
return 0;
}Instructions
▼ ← Click the triangle to hide or reveal instructions.Task Incomplete
Editor Input:
Editor Output:
string type.string city = "Paris"; stores the text Paris.string country = "France"; stores the text France.cout << city << ", " << country; prints both strings with a comma between them.Paris, France.Test Incomplete