Computer Programming 简明教程
Computer Programming - Variables
变量是您赋予计算机内存位置的名称,这些内存位置用于在计算机程序中存储值。
Variables are the names you give to computer memory locations which are used to store values in a computer program.
例如,假设您想在您的程序中存储两个值 10 和 20,并且在稍后的阶段,您希望使用这两个值。让我们看看您将如何做到这一点。以下是以下三个简单步骤 -
For example, assume you want to store two values 10 and 20 in your program and at a later stage, you want to use these two values. Let’s see how you will do it. Here are the following three simple steps −
-
Create variables with appropriate names.
-
Store your values in those two variables.
-
Retrieve and use the stored values from the variables.
Creating variables
在 C 编程中,创建变量也称为 declaring variables 。不同的编程语言有不同的方式在程序内创建变量。例如,C 编程有以下简单的方法来创建变量:
Creating variables is also called declaring variables in C programming. Different programming languages have different ways of creating variables inside a program. For example, C programming has the following simple way of creating variables −
#include <stdio.h>
int main() {
int a;
int b;
}
上面的程序创建了两个变量,以保留两个内存位置,名称为 a 和 b 。我们使用 int 关键字创建了这些变量,以指定变量 data type ,这意味着我们要在这些两个变量中存储整数。同样,你可以创建变量来存储 long 、 float 、 char 或任何其他数据类型。例如:
The above program creates two variables to reserve two memory locations with names a and b. We created these variables using int keyword to specify variable data type which means we want to store integer values in these two variables. Similarly, you can create variables to store long, float, char or any other data type. For example −
/* variable to store long value */
long a;
/* variable to store float value */
float b;
你可以通过将变量放在单行中,但用逗号分隔来创建类似类型的变量,如下所示:
You can create variables of similar type by putting them in a single line but separated by comma as follows −
#include <stdio.h>
int main() {
int a, b;
}
下面列出的是你需要记住的有关变量的关键要点:
Listed below are the key points about variables that you need to keep in mind −
-
A variable name can hold a single type of value. For example, if variable a has been defined int type, then it can store only integer.
-
C programming language requires a variable creation, i.e., declaration before its usage in your program. You cannot use a variable name in your program without creating it, though programming language like Python allows you to use a variable name without creating it.
-
You can use a variable name only once inside your program. For example, if a variable a has been defined to store an integer value, then you cannot define a again to store any other type of value.
-
There are programming languages like Python, PHP, Perl, etc., which do not want you to specify data type at the time of creating variables. So you can store integer, float, or long without specifying their data type.
-
You can give any name to a variable like age, sex, salary, year1990 or anything else you like to give, but most of the programming languages allow to use only limited characters in their variables names. For now, we will suggest to use only a….z, A….Z, 0….9 in your variable names and start their names using alphabets only instead of digits.
-
Almost none of the programming languages allow to start their variable names with a digit, so 1990year will not be a valid variable name whereas year1990 or ye1990ar are valid variable names.
每种编程语言都提供了更多有关变量的规则,在你详细了解编程语言后,你会了解它们。
Every programming language provides more rules related to variables and you will learn them when you will go in further detail of that programming language.
Store Values in Variables
你在上一节中了解了如何创建变量。现在,我们将在这些变量中存储一些值 −
You have seen how we created variables in the previous section. Now, let’s store some values in those variables −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
}
上面的程序有两个额外的语句,我们在语句中将 10 存储到变量 a 中,将 20 存储到变量 b 中。几乎所有编程语言都有类似于在变量中存储值的存储方式,我们将在等号 = 的左侧保留变量名,无论我们想在变量中存储什么值,我们都将该值保存在右侧。
The above program has two additional statements where we are storing 10 in variable a and 20 is being stored in variable b. Almost all the programming languages have similar way of storing values in variable where we keep variable name in the left hand side of an equal sign = and whatever value we want to store in the variable, we keep that value in the right hand side.
现在,我们已经完成了两个步骤,首先创建两个变量,然后在这些变量中存储所需的值。现在,变量 a 的值为 10,变量 b 的值为 20。换句话说,当执行上述程序时,名为 a 的内存位置将持有 10,名为 b 的内存位置将持有 20。
Now, we have completed two steps, first we created two variables and then we stored required values in those variables. Now variable a has value 10 and variable b has value 20. In other words we can say, when above program is executed, the memory location named a will hold 10 and memory location b will hold 20.
Access stored values in variables
如果我们在变量中不使用存储的值,那么创建变量和在其中存储值就没有意义。我们知道上面的程序有两个变量 a 和 b ,它们分别存储值 10 和 20。因此,我们尝试打印存储在两个变量中的值。以下是一个 C 程序,它打印存储在变量中的值 −
If we do not use the stored values in the variables, then there is no point in creating variables and storing values in them. We know that the above program has two variables a and b and they store the values 10 and 20, respectively. So let’s try to print the values stored in these two variables. Following is a C program, which prints the values stored in its variables −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d\n", a );
printf( "Value of b = %d\n", b );
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
你一定在上一章中看到了 printf() 函数,我们曾用它来打印“Hello, World!”。这一次,我们使用它来打印变量的值。我们正在使用 %d ,它将在 printf() 语句中被变量的值替换。我们可以使用以下方式使用单个 printf() 语句打印两个值 −
You must have seen printf() function in the previous chapter where we had used it to print "Hello, World!". This time, we are using it to print the values of variables. We are making use of %d, which will be replaced with the values of the given variable in printf() statements. We can print both the values using a single printf() statement as follows −
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d and value of b = %d\n", a, b );
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of a = 10 and value of b = 20
如果你想在 C 编程中使用 float 变量,你需要使用 %f 来替代 %d ,如果你想打印一个字符值,则需要使用 %c 。类似地,可以使用不同的 % 和字符来打印不同的数据类型。
If you want to use float variable in C programming, then you will have to use %f instead of %d, and if you want to print a character value, then you will have to use %c. Similarly, different data types can be printed using different % and characters.
Variables in Java
以下是用 Java 编程语言编写的等效程序。该程序将使用类似于 C 编程的创建一个变量 a 和 b ,并分别赋予它们值 10 和 20,最后以两种方式打印这两个变量的值 -
Following is the equivalent program written in Java programming language. This program will create two variables a and b and very similar to C programming, it will assign 10 and 20 in these variables and finally print the values of the two variables in two ways −
public class DemoJava {
public static void main(String []args) {
int a;
int b;
a = 10;
b = 20;
System.out.println("Value of a = " + a);
System.out.println("Value of b = " + b);
System.out.println("Value of a = " + a + " and value of b = " + b);
}
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
Variables in Python
以下是使用 Python 编写的等效程序。该程序将创建一个变量 a 和 b ,并同时赋予它们值 10 和 20。
Following is the equivalent program written in Python. This program will create two variables a and b and at the same time, assign 10 and 20 in those variables.
Python 不需要你在创建变量时指定数据类型,也不需要提前创建变量。
Python does not want you to specify the data type at the time of variable creation and there is no need to create variables in advance.
a = 10
b = 20
print "Value of a = ", a
print "Value of b = ", b
print "Value of a = ", a, " and value of b = ", b
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of a = 10
Value of b = 20
Value of a = 10 and value of b = 20
你可以在 C 和 Java 编程中使用以下语法在声明变量的同时赋值 -
You can use the following syntax in C and Java programming to declare variables and assign values at the same time −
#include <stdio.h>
int main() {
int a = 10;
int b = 20;
printf( "Value of a = %d and value of b = %d\n", a, b );
}
当执行上述程序时,它将生成以下结果 −
When the above program is executed, it produces the following result −
Value of a = 10 and value of b = 20