学习要点

  1. 什么是变量?为什么需要变量?
  2. 变量的赋值、重新赋值。
  3. 初步了解变量的数据类型。

代码示例

#include<iostream>

int main(){
    int score = 0;  //【知识点】初始赋值
    int lives = 3;
    std::cout<< "score: " << score << std::endl; //【知识点】显示变量
    std::cout<< "lives: " << lives << std::endl;
    score = 5; //【知识点】重新赋值
    std::cout<< "score: " << score << std::endl;
    return 0;
} 

作业

  1. 用一个变量存储冬季奥运会的举办城市。通过修改该变量,打印23届、24届冬季奥运会的举办城市。

Introduction

Computer programs instruct computers how to process data.

Sometimes the data comes from a user, like when a game asks you to pick a difficulty level and music volume:

A diagram of a laptop with a game settings interface

Other times, the data is already stored in the program, like when that same game starts off with a score of 0 and 3 lives:

A diagram that starts with a table of 2 rows.

Either way, the program can store that data in variables. Each variable has a name, a value, and a type. The value might change over time, and that’s why its “variable.”

That game is using at least four variables:

name value type
volume 3 number
difficulty “easy” string
score 0 number
lives 3 number

Many variables store numbers and strings, like the ones above. Variables can also store other types of data, like lists, dictionaries, and Boolean values (true/false).

We’ll start by learning numbers and strings, and dive into advanced types later. First things first, let’s write some code!

Assigning variables

Here’s how we create a variable named score in C++:

int score = 0;

That line of code is called a statement. All programs are made up of statements, and each statement is an instruction to the computer about something we need it to do.

Let’s add the lives variable:

int score = 0;  
int lives = 3;

Now our code is using two statements to store two variables with two different values. Behind the scenes, the computer is storing each value in a different place in memory and the variables point at that memory location.

Displaying variables

How do we make sure the computer is actually storing those values in memory? We can ask it to display the values.

int score = 0;  
int lives = 3;  
std::cout<< score << std::endl;
std::cout<< lives << std::endl;

When we instruct the computer to print score, the computer has to look in its memory for the current value of that variable, and then once it finds it, it can display it in the console.

Re-assigning variables

In a game, the player’s score and their number of lives don’t stay the same. The score typically goes up, and the lives typically go down. That means we need to be able to change the value of those variables later.

We can re-assign a variable to a new value using code similar to our initial assignment:

score = 5;

Now the score variable is storing the value 5 instead of the initial value of 0.

Notice that when we reassign in JavaScript, we no longer put the var in front. That’s not the case in all languages, so it may be slightly different in another language you’re learning.

Here’s a program that creates the two variables, re-assigns one of them, and displays values along the way:

#include<iostream>

int main(){
    int score = 0;  //【知识点】初始赋值
    int lives = 3;
    std::cout<< "score: " << score << std::endl; //【知识点】显示变量
    std::cout<< "lives: " << lives << std::endl;
    score = 5; //【知识点】重新赋值
    std::cout<< "score: " << score << std::endl;
    return 0;
} 

This program is interesting because it contains 2 lines that look exactly the same: cout « score. Yet all of the outputted lines are different; how can that be?

That’s because the computer runs each statement in order, and the value of the score variable changes over time.

Let’s step through this program, one statement at a time:

Step Statement Description
1 int score = 0; Initializes the variable score to 0
2 int lives = 3; Initializes the variable lives to 3
3 cout « score; Displays current value of score: 0
4 cout « lives; Displays current value of lives: 3
5 score = 5; Re-assigns the variable score to the value 5
6 cout « score; Displays current value of score: 5

When we’re first developing a program, we often display the value of variables to double-check the state of the program. Are the variables storing what we think they’re storing, or did our code do something unexpected along the way?

(Optional)Pseudocode for variables

This pseudocode represents assigning a variable:

a ← expression

Whenever you see that pseudocode, it means that the variable a (or whatever it’s called) is assigned the value of expression.

For example, you might see pseudocode like this:

age ← 21

That means the variable age is assigned the value 21.

Let’s look at the equivalent code in a few textual languages:

language code
JavaScript var age = 21;
Python age = 21
C++ int age = 21;

Variable assignment is also a concept in block-based languages:

language block
Snap! Screenshot of variable block from Snap, says "set age to 21"
AppInventor Screenshot of variable block from AppInventor

As you can see, the code to assign variables is very similar across languages. There are some differences though, and that’s why we use pseudocode: to communicate the concept and not worry about the exact syntax.

Q: What’s the pseudocode equivalent of this C++?

int x = 200;

Choose 1 answer:

A. var x = 200
B. x ← 200
C. x = 200
D. var x ← 200

Data Types

Ref: C++ Data Types

DATA TYPE SIZE (IN BYTES) RANGE
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65,535
unsigned int 4 0 to 4,294,967,295
int 4 -2,147,483,648 to 2,147,483,647
long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
long long int 8 -(2^63) to (2^63)-1
unsigned long long int 8 0 to 18,446,744,073,709,551,615
signed char 1 -128 to 127
unsigned char 1 0 to 255
float 4  
double 8  
long double 12  
wchar_t 2 or 4 1 wide character

不同类型的数值输入:

//长整型的输入
long long x1 = 8LL; //数字后加上LL表示按照长整数的精度初始化或者计算
cin >> x1;          //c++的一般输入方法
scanf("%lld", &x1); //C语言风格的输入方法。

Note : Above values may vary from compiler to compiler. In above example, we have considered GCC 64 bit.

本页面采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。
致谢:Khan Academy - AP® Computer Science Principles