计算机程序是一系列的指令,告诉计算机做什么。
语句和表达式
最常见的类型,在程序指令语句 。 在C + +中的声明是语言中最小的独立单元。 在人类的语言,它是类似于一个句子。 我们写的句子,以传达一个想法。 在C + +中,我们写的报表为了传达给编译器,我们要执行任务。 报表在C + +以分号结束 。
有许多不同种类的报表,C + +。 以下是一些简单的语句的最常见的类型:
1
2
3
int x;
x = 5;
cout << x;
int x
is a declaration statement . It tells the compiler that x is a variable. All variables in a program must be declared before they are used. We will talk more about variables shortly.
x = 5
is an assignment statement . It assigns a value (5) to a variable (x).
cout << x;
is an output statement. It outputs the value of x (which we set to 5 in the previous statement) to the screen.
The compiler is also capable of resolving expressions. An expression is an mathematical entity that evaluates to a value. For example, in math, the expression 2+3 evaluates to the value 5. Expressions can involve values (such as 2), variables (such as x), operators (such as +) and functions (which return an output value based on some input value). They can be singular (such as 2, or x), or compound (such as 2+3, 2+x, x+y, or (2+x)*(y-3)).
For example, the statement x = 2 + 3;
is a valid assignment statement. The expression 2+3 evaluates to the value of 5. This value of 5 is then assigned to x.
Functions
In C++, statements are typically grouped into units called functions. A function is a collection of statements that executes sequentially. Every C++ program must contain a special function called main() . When the C++ program is run, execution starts with the first statement inside of main(). Functions are typically written to do a very specific job. For example, a function named Max() might contain statements that figures out which of two numbers is larger. A function named CalculateGrade() might calculate a student’s grade. We will talk more about functions later.
Libraries
Libraries are groups of functions that have been “packaged up” for reuse in many different programs. The core C++ language is actually very small and minimalistic — however, C++ comes with a bunch of libraries, known as the C++ standard libraries, that provide programmers with lots of extra functionality. For example, the iostream library contains functions for doing input and output. During the link stage of the compilation process, the libraries from the C++ standard library are the runtime support libraries that are linked into the program (this will be discussed further in lesson 1.4).
Taking a look at a sample program
Now that you have a brief understanding of what statements, functions, and libraries are, let’s look at a simple hello world program.
Consider our hello world program: