第7章高水準プログラミング言語
7.2 Programming Language Constructs7.2 プログラミング言語の構成要素
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Discuss and compare HLL data types
• HLLのデータ型を論じ、比較する。
• Demonstrate the use of variables
• 変数の使い方を実演する。
• Examine HLL expressions and statements
• HLLの式と文を調べる。
• Describe the implementation of flow of control in HLLs
• HLLでの制御フローの実装を説明する。
• Introduce the concept of functions
• 関数の概念を紹介する。
• Classify well-structured programs
• よく構造化されたプログラムを分類する。
• Explain the concept of exception handling
• 例外処理の概念を説明する。
• Summarize files and input/output
• ファイルと入出力をまとめる。
HLLs exist to communicate to a computer the logical steps for approaching a given task or application, and many HLLs act the same. Because of this, once you have mastered a modern HLL, it becomes easier to learn additional languages since you now know the correct questions to ask. For example, a starting point might be to find out how to obtain a simple program output which allows you to see how to run a program and test the concepts we are about to learn.
英語のヒント
HLLは、特定のタスクや用途に取り組むための論理的な手順をコンピューターへ伝えるために存在し、多くのHLLは同じように振る舞う。そのため、現代のHLLを一つ習得すると、何を尋ねればよいかが分かるようになり、別の言語を学びやすくなる。たとえば、簡単なプログラムの出力を得る方法を調べることが、出発点になりうる。それによって、プログラムの動かし方を理解し、これから学ぶ概念を試せる。
In this section we will describe the structural concepts of HLLs to give us the tools with which to compare them and learn them in a consistent way. A good starting point to examine programming language constructs is to demonstrate the fundamental building blocks of HLLs. These include the data types that languages can legally manipulate, how they store such data, how they structure the expressions and statements by which they communicate, and the control of the programming flow.
英語のヒント
この節では、HLLを一貫した方法で比較し、学ぶための道具を得られるように、HLLの構造上の概念を説明する。プログラミング言語の構成要素を調べるには、まずHLLの基本的な部品を示すとよい。これには、言語が規則上操作できるデータ型、そうしたデータの保存方法、伝達に使う式や文の構成方法、プログラムの流れの制御が含まれる。
HLL Data TypesHLLのデータ型
The data types of a language form the legal set of the kinds of data which an HLL may manipulate. These data types may be very simple, or they may be more complex. The simplest data type of a language is a primitive data type (also, basic data type), for example, integers and char in the C programming language. Data corresponding to variables of these types can usually be represented and manipulated directly using the machine hardware both in memory and via registers.
英語のヒント
言語のデータ型は、そのHLLが規則上操作できるデータの種類の集合を形作る。非常に単純な型もあれば、より複雑な型もある。最も単純なものを、プリミティブデータ型、または基本データ型という。たとえば、C言語の整数やcharである。これらの型の変数に対応するデータは、通常、メモリ内でもレジスタを通しても、マシンのハードウェアを使って直接表現・操作できる。
However, languages usually contain complex data types as well. A complex data type consists of multiple primitive types that are used as their building blocks. An example of this is the string data type which represents a sequence of characters. In the C programming language, character strings are complex data types represented using arrays of characters. In JavaScript, string is a primitive data type. Figure 7.9 relays the various data types.
英語のヒント
ただし、言語には、通常、複合データ型もある。複合データ型は、構成部品となる複数の基本型からなる。一例は、文字の並びを表す文字列型である。C言語では、文字列は、文字の配列で表す複合データ型である。JavaScriptでは、stringは基本データ型である。図7.9は、さまざまなデータ型を示す。

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典
Figure 7.9 JavaScript data types are divided into primitive and complex types. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図7.9 JavaScriptのデータ型は、基本型と複合型に分けられる。(出典:著作権 Rice University、OpenStax。CC BY 4.0ライセンス)
In general, data types are collections of values from a given domain: the JavaScript number data type covers the domain of floating-point number values that can be represented in 64 bits. It also consists of a legal set of well-defined operations that may be performed on the values of the domain covered by the number data type. The operations on these numbers in JavaScript are defined by the arithmetic operators of the language.
英語のヒント
一般に、データ型は、ある定義域に属する値の集合である。JavaScriptのnumber型は、64ビットで表現できる浮動小数点数の値の領域を扱う。また、number型が扱う領域の値に対して実行できる、明確に定められた、規則上有効な操作の集合も含む。JavaScriptでこれらの数に行う操作は、言語の算術演算子によって定められる。
Some languages separate numbers into integer types (whole numbers) and floating point types (decimal numbers). Even among similar languages such as C++ and Java, there may be different numbers of primitive data types.
英語のヒント
数を、整数型と浮動小数点型(小数)に分ける言語もある。C++とJavaのように似た言語同士でも、基本データ型の数が異なる場合がある。
Primitive Data Types基本データ型
These data types are considered primitive because they relate very closely to the machine hardware. This means that the format, or bit pattern, of the actual values can be recognized by the registers and arithmetic-logic unit (ALU) of the computer. Some examples of primitive data types are number, character, and Boolean (hold the values true or false), as visible in Table 7.2.
英語のヒント
これらのデータ型を基本型とみなすのは、マシンのハードウェアと非常に密接に関係するからである。つまり、実際の値の形式、すなわちビットパターンを、コンピューターのレジスタや算術論理演算装置(ALU)が認識できる。基本データ型の例には、表7.2のように、数値、文字、真偽値型(trueまたはfalseを持つ)がある。
Java | C++ | Size サイズ | Value Range 値の範囲 |
|---|---|---|---|
short int | short int | 2 bytes 2バイト | –32,768 to +32,767 –32,768~+32,767 |
unsigned short int | 2 bytes 2バイト | 0 to +65,535 0~+65,535 | |
int | int | 4 bytes 4バイト | –2,147,483,648 to +2,147,483,647 –2,147,483,648~+2,147,483,647 |
unsigned int | 4 bytes 4バイト | 0 to +4,294,967,295 0~+4,294,967,295 | |
long int | long int | 4 bytes 4バイト | –2,147,483,648 to +2,147,483,647 –2,147,483,648~+2,147,483,647 |
unsigned long int | 4 bytes 4バイト | 0 to +4,294,967,295 0~+4,294,967,295 | |
long long int | unsigned long long int | 8 bytes 8バイト | –9,223,372,036,854,775,808 to 9,223,372,036,854,775,808 –9,223,372,036,854,775,808~9,223,372,036,854,775,808 |
unsigned long long int | 8 bytes 8バイト | 0 to 18,446,744,073,709,551,615 0~18,446,744,073,709,551,615 |
Table 7.2 Contrast Between Integer Data Types in Java and C+
表7.2 JavaとC+の整数データ型の対比
We learned that strong typing refers to the characteristic of an HLL in which a variable is restricted to holding values of the type with which it is defined. The concept of coercion refers to the ability of a variable of a data type to be forced to hold a value of a different data type. In other words, coercion rules are a relaxation of type checking. For example, a Java int data type holds a whole number of size four bytes, while a short int holds a whole number of two bytes. We can legally assign the value of the short int to the int: it is coerced by the assignment, which makes absolute sense because the short value can fit into the longer value.
英語のヒント
強い型付けとは、変数が、定義された型の値だけを保持するよう制限される、HLLの特徴であると学んだ。型の強制変換は、あるデータ型の変数に、異なるデータ型の値を強制的に持たせられることを指す。言い換えると、強制変換の規則は、型検査を緩めるものである。たとえば、Javaのint型は4バイトの整数を、short intは2バイトの整数を保持する。short intの値は、規則上、intへ代入できる。代入によって強制変換されるのである。shortの値は、より長い値の中に収まるので、これは十分に理にかなっている。
On the other hand, we cannot assign a Java 8-byte long data type to a Java 4-byte int; it is too big to fit, and if we try, we will get a compile time error that will not allow the program to run. However, we can coerce the assignment by using a mechanism called a type cast. This is a mechanism in many HLLs which allows us to force the larger value into the smaller space given to us by the smaller variable. This can have side effects, which must be known by the programmer to use the mechanism effectively. The side effect of the Java long to int example is truncation: four of the bytes are dropped.
英語のヒント
一方、Javaの8バイトのlong型を、4バイトのintへ代入することはできない。大きすぎて収まらず、試すとコンパイル時エラーになり、プログラムを実行できない。しかし、型キャストという仕組みを使えば、代入を強制できる。これは、多くのHLLにある仕組みで、小さい変数に与えられた領域へ、より大きい値を強制的に入れられる。この操作には副作用があり、有効に使うために、プログラマーはそれを知っていなければならない。Javaのlongからintへの変換では、切り捨てが副作用となり、4バイト分が削られる。