All lessons

2. Variables and types

Variables and types

0 of 5 activities0%

Reading 1

A variable has a type and a name

Open

A variable is a named place in memory. In C++ you must declare the type before you use the name.

Common types: - int for whole numbers (20, -3) - double for numbers with a fractional part (3.14) - char for a single character ('A') - bool for true or false

You can declare and assign in one step: int age = 20;

int count = 0;
double price = 9.99;
char grade = 'A';
bool done = false;

Check 2

Pick a type

Open

Which type is the best fit for the value 3.14?

Fill in 3

Integer keyword

Open

The C++ keyword for an integer variable is

Try it 4

Declare and print

Open

Run the program. Change the values and print another variable.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Store and print an age

Open

Declare an int named age with the value 20. Print only that number and a newline.

main.cpp
Loading editor…