Transcript Document

Chapter 9
Programming
Languages
OBJECTIVES
After reading this chapter, the reader should
be able to:
Have a vision of computer language evolution.
Distinguish between machine, assembly, and high-level
languages.
Understand the process of creating and running a program.
Distinguish between the different categories of languages:
procedural, object-oriented, functional, declarative, and special.
Become familiar with elements of the procedural language C.
Contents
9.1 Evolution
9.2 Building a Program
9.3 Program Execution
9.4 Categories of Language
9.5 A procedural Language:C
Summary
9.1
EVOLUTION
• Programming language:程序设计语言。
• evolution:演化
• Computer language :计算机语言。根
据预先定义的规则(语法syntax)而写出的
预定语句的结合,这些语句组成程序。
Figure 9-1
Evolution of computer languages
• Machine language :机器语言,由“0”和
“1”的字符串组成。
Note:
The only language
understood by a computer is
machine language.
Program 9.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Program in machine language
00000000
00000100
0000000000000000
01011110 00001100110000100000000000000010
11101111 000101100000000000000101
11101111 10011110 0000000000001011
11111000 10101101
11011111 0000000000010010
0110001011011111 0000000000010101
11101111 00000010
11111011 0000000000010111
11110100 1010110111011111 0000000000011110
0000001110100010
11011111 0000000000100001
11101111 00000010
11111011 0000000000100100
01111110 11110100 10101101
11111000 10101110 110001010000000000101011
0000011010100010
11111011 0000000000110001
11101111 00000010
11111011 0000000000110100
00000100
0000000000111101
00000100
0000000000111101
• Symbolic language:符号语言。
assembler(汇编程序):将符号代码翻译为机
器语言的特定程序。
Assembly language(汇编语言)
Program 9.2 Program in symbolic language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Entry main, ^m<r2>
subl2 #12,sp
jsb
C$MAIN_ARGS
movab
$CHAR_STRING_CON
pushal
-8(fp)
pushal
(r2)
calls #2,read
pushal
-12(fp)
pushal
3(r2)
calls #2,read
mull3
-8(fp),-12(fp),pushal
6(r2)
calls #2,print
clrl
r0
ret
• High-level language:高级语言
compilation(编译):高级语言转化为机器语言的
过程。
Basic、Pascal、C、C++、Java
• Natural language:自然语言。
Program 9.3 Program in C language
/*
This program reads two integer numbers from the
keyboard and prints their product.
*/
#include <stdio.h>
int main (void)
{/*Local Declarations */
int number1, number2;
int result;
// Statements */
scanf(“%d,%d”,&number1,&number2);
result = number1 * number2;
printf(“The result is:%d”, result);
return 0;
}
9.2
BUILDING
A
PROGRAM
Key terms
• Building a program:构建程序。
• Executable file (machine language):可执行
文件。
(1)writing and editing the program :编写和编
辑程序
(2)compiling the program :编译程序
(3)Linking the program with the required
library modules:用所需的库模块链接程序
Key terms
•
•
•
•
•
Source file:源文件
preprocessor:预处理程序
Translator:翻译程序
Object module:目标模块
Linker:链接器
Figure 9-2
Building a program
9.3
PROGRAM
EXECUTION
Figure 9-3
Program execution
载入
9.4
CATEGORIES
OF
LANGUAGES
Key terms
•
•
•
•
•
•
Categories of languages:语言的分类。
procedural:过程化语言
Object-Oriented :面向对象的语言
Functional :函数型语言
Declarative:说明型语言
Special:专用语言
Figure 9-4
Categories of languages
9.4 categories of Languages
• Procedural (imperative)languages
– Fortran、Cobol、Pascal、C、Ada
• Object-Oriented
– C++、Java
• Encapsulation:封装
• Inheritance:继承
• Polymorphism:多态
Figure 9-5
Function in a functional language
Figure 9-6
Extracting the third element of a list
Functional language:
Lisp
Scheme
see page 176
9.4 categories of Languages-Declarative
• 说明性语言(declarative language )依据逻
辑推理的原则回答查询。
• First-order predicate calculus:一阶谓词演
算
• See Page 176
9.4 categories of Languages-special
• HTML(Hypertext markup language):由格
式标记(formatting hints)+超链接(links)组成
的伪语言。
• Head:文件头。包含页面标题和其他浏
览器将要用到的参数。
• Body:主体。页面的实际内容。包含文
本(text)和标记(tags)。
• Tags:嵌入到文本中的符号。
Table 9.1 Common tags
Beginning Tag
---------------<HTML>
<HEAD>
<BODY>
<TITLE>
<Hi>
<B>
<I>
<U>
<SUB>
<SUP>
<CENTER>
<BR>
<OL>
<UL>
<LI>
<IMG>
<A>
Ending Tag
---------------</HTML>
</HEAD>
</BODY>
</TITLE>
</Hi>
</B>
</I>
</U>
</SUB>
</SUP>
</CENTER>
</OL>
</UL>
</LI>
</A>
Meaning
------------------------document
document head
document body
document title
different header levels
boldface
Italic
underlined
subscript
superscript
centered
line break
ordered list
unordered list
an item in the list
an image
an address (hyperlink)
Program 9.4
HTML Program
<HTML>
<HEAD>
<TITLE> Sample Document </TITLE>
</HEAD>
<BODY>
This is the picture of a book:
<IMG SRC="Pictures/book1.gif" ALIGN=MIDDLE>
</BODY>
</HTML>
9.5
A PROCEDURAL
LANGUAGE:
C
Key terms
(Identifiers)
• Identifiers:标识符。
reserved words:保留字
keywords:关键字
• Data types:数据类型
standard types: int char float
Derived types(派生类型):
pointer(指针)、
enumerated type(枚举类型)、
union(联合)、
array(数组)、
structure(结构)
Figure 9-7
Variables(变量)
Key terms
(Variable)
• Variable declaration and definition:变
量声明和定义。
float price;
• Variable Initialization :变量初始化
float price=23.45;
Key terms (Constants)
• Constants:常量。在程序执行过程中不能
更改的数据。
• Literal Constant :文字常量
Circumference=2*length*width
• Named Constant:命名常量
const pi=3.14;
• Symbolic Constant:符号常量
#define taxrate 0.0825
Key terms (Constants)
• Constants and Types:常量和类型。
23
23.12
'A'
"Hello "
/*Integer literal*/
/*Floating-point literal*/
/*Character literal*/
/*String literal*/
Key terms (Input and Output)
• Input:
scanf(" %d ",&num);
• Output:
printf(" The value is: %d",&num);
Key terms (Expressions)
• operator:运算符
Arithmetic operator:算术运算符
relational operator:关系运算符
logical operator:逻辑运算符
assignment operator:赋值运算符
• operand:操作数
3+4
Table 9.2 Arithmetic operators
Operator
----------+
*
/
%
----++
--
Example
Definition
----------------------------3 + 5
Addition
2 - 4
Subtraction
Num * 5
Multiplication
Division (quotient) Sum / Count
Division (remainder) Count % 4
----------------------------Count ++
Increment
Count -Decrement
Table 9.3 Relational operators
Operator
--------<
<=
>
>=
==
!=
Definition
---------------Less than
Less than or equal to
Greater than
Greater than or equal to
Equal to
Not equal to
Example
------------Num1 < 5
Num1 <= 5
Num2 > 3
Num2 >= 3
Num1 == Num2
Num1 != Num2
Table 9.4 Logical operators
Operator
---------!
&&
||
Definition
-----------NOT
AND
OR
Example
---------------------- ----- ----! ( Num1 < Num2 )
(Num1 < 5 ) && (Num2 > 10 )
(Num1 < 5 ) || (Num2 > 10 )
Table 9.5 Assignment operators
Operator
----------=
+=
-=
*=
/=
%=
Example
-------------Num
=5
Num += 5
Num -= 5
Num *= 5
Num /= 5
Num %= 5
Meaning
---------------------Store 5 in Num
Num = Num + 5
Num = Num - 5
Num = Num * 5
Num = Num / 5
Num = Num % 5
Key terms (Statements)
• Statements :语句
Expression statement:表达式语句。
b=a+4;
b+c*4;
Compound statement:复合语句
{ x=1;
y=20;
}
Selection statement:选择语句
Iterative statement:循环语句
Jump statement:跳转语句
Figure 9-8
Statements
Figure 9-9
Side effect of a function
Functions
Figure 9-10
Function declaration(函数声明)
main()
{
printf(“This is a c program.”);
}
This is a c program
main()
/*求两数之和*/
{int num1,num2,result;
num1=12;
num2=23;
result=num1+num2;
printf(“Sum is %d”,result);
}
Sum is 35
18,20
This largest is 20
int max(int x,int y);
main()
{int num1,num2,largest;
scanf(“%d,%d”,&num1,&num2);
Largest=max(num1,num2);
printf(“The largest is %d”,largest);
}
int max(int x,int y)
{int z;
if(x>y) z=x;
else z=y
return(z);}
Figure 9-11
if-else statement
Selection
Figure 9-12
switch statement
Selection
Figure 9-13
while loop
repetition
Figure 9-14
for loop
Figure 9-15
do-while loop
Summary
• 计算机能理解的唯一语言是机器语言(machine
language).
• 符号语言使用符号来表示各种机器语言指
令,符号语言也称为汇编语言。
• 高级语言可以使程序员避免考虑硬件。
• 构建程序的步骤包括编写、编辑、编译和链
接代码。
• 有5种计算机语言:过程化语言、面向对象的
语言、函数型语言、说明性语言、专用语言。
Summary
• 标识符在程序中用来命名数据和其他对象.
• 数据类型定义了值的集合和可以应用于这些
值的指令集合。
• C语言中的标准数据类型有整型(int),字符型
(char)、浮点型(float)。
• 常量就是在执行过程中不可改变的数据值。
• 变量是存储单元的名字。
Summary
• 表达式是一系列操作数和运算符的组合.
• 语句使程序执行动作。
• 函数是执行特定任务的独立模块。
• C语言有一个称为main的主函数和0个或多个
其他函数。
• 变量是存储单元的名字。
Summary
• C中if-else语句或switch语句可以完成多
个判断。
• C中的循环可以通过while、for、do-while
来实现。