プリンキピア

第4章アルゴリズムの言語による実現:低水準プログラミング言語

LINK TO LEARNING学習リンク
The benefit of using these statements is that they make the flow of execution clear in the source code. When writing an “if,” for instance, it is clear which code is inside the “if” and which is outside. And when mixing an “if” with a “for” loop, it is clear whether the “if” is inside the “while” or vice-versa. These sorts of inside/outside relationships are difficult to perceive in unstructured code. In a conditional statement like “if”, the compiler executes a line if the condition has been met or is true. Otherwise, it moves to the next statement:
英語のヒント
これらの文を使う利点は、実行の流れがソースコードで明確になることである。たとえばifを書けば、どのコードがその内側にあり、どれが外側かがわかる。またifとforループを組み合わせたとき、ifがwhileの内側か、その逆かが明確になる。このような内外の関係は、非構造化コードでは捉えにくい。ifのような条件文では、条件が満たされる、あるいは真である場合に、コンパイラーがその行を実行する。そうでなければ次の文へ進む。
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter an input value: ");
        int Val = s.nextInt();
        int Curr_Val = 10;

        if (Val > Curr_Val) {
            System.out.println("The Value that you entered is greater than the current.");
        } else {
            System.out.println("The Current value is greater than the value that you entered.");
        }

    }
}
Inside a loop, like “while”, the statements are executed only if the condition in the loop is true. Otherwise, the loop execution terminates, and the compiler moves to the statements after the loop:
英語のヒント
whileのようなループ内では、ループの条件が真の場合にだけ、文が実行される。そうでなければループの実行が終了し、コンパイラーはループの後の文へ進む。
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter an input value: ");
        int Val = s.nextInt();
        int Prod = 1;

        while (Val != 0) {
            Prod = Prod * Val;
            Val--;
        }

        System.out.println("The factorial is " + Prod);
    }
}
There is a substantial upside to making a language structured, and the only significant downside is that it makes the language a bit more high-level. Therefore, among programming languages that are currently in widespread use, all the middle-level and high-level languages are structured.
英語のヒント
言語を構造化する利点は大きく、主な欠点は、言語が少し高水準になることだけである。そのため、現在広く使われるプログラミング言語では、中水準と高水準の言語はすべて構造化されている。
Procedural Programming手続き型プログラミング
In a procedural language, each part of the program is a procedure, which is a function in the context of programming. Known as procedural programming, this is a paradigm in which code is organized into procedures (Figure 4.12). It is a sub-type of imperative programming. All procedural languages, then, are imperative, but not all imperative languages are procedural. A programmer designs each procedure to accomplish a specific task and gives it a descriptive name. This allows the programmer to break a large and complicated program into smaller, more manageable pieces, which are easier to write and easier for other programmers to understand. This property of code being divided into small, reusable piece is called modularity, and it is considered a virtue.
英語のヒント
手続き型言語では、プログラムの各部分は、プログラミングの文脈では関数に当たる手続きである。手続き型プログラミングと呼ばれるこのパラダイムでは、コードを手続きに整理する(図4.12)。命令型プログラミングの一種なので、手続き型言語はすべて命令型だが、すべての命令型言語が手続き型とは限らない。プログラマーは、各手続きが特定の仕事をするよう設計し、内容がわかる名前を付ける。これにより、大きく複雑なプログラムを、より小さく管理しやすい部分に分け、書きやすく、ほかのプログラマーにも理解しやすくできる。コードを小さく再利用可能な部分へ分けるこの性質は、モジュール性と呼ばれ、長所とみなされる。
Figure 4.12

attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license · CC BY 4.0 · Source出典

Figure 4.12 This procedural programming comparison relates a real-life scenario, such as making a sandwich, to the corresponding C++ version of the same scenario. In both cases, each step has its own procedure. (attribution: Copyright Rice University, OpenStax, under CC BY 4.0 license)
図4.12 手続き型プログラミングの比較として、サンドイッチ作りのような現実の場面と、それに対応するC++での表現を関連付けている。どちらの場合も、各段階に専用の手続きがある。(出典表記:著作権者 Rice University、OpenStax、CC BY 4.0ライセンス)
For example, in C, a procedure that opens a socket, or an Internet connection between two computers, is called g_socket_connect()(Figure 4.13). This procedure involves executing a series of imperative commands that use operating system features (such as the transport layer), system calls, and networking hardware (such as network interface cards, or NICs), to set up a socket connection. To close that connection and end communication, C uses the g_socket_close() procedure, which executes a series of commands that shut down the connection. These procedures use the imperative approach to accomplish their respective tasks, so each function contains a series of imperative statements.
英語のヒント
たとえばCでは、2台のコンピューター間のインターネット接続であるソケットを開く手続きを、g_socket_connect()と呼ぶ(図4.13)。この手続きでは、オペレーティングシステムの機能(トランスポート層など)、システムコール、ネットワークのハードウェア(ネットワークインターフェースカード、NICなど)を利用する命令を順に実行し、ソケット接続を確立する。接続を閉じて通信を終えるには、g_socket_close()という手続きを使う。これは接続を終了する一連の命令を実行する。これらの手続きは、それぞれの仕事を命令型の方法で行うため、各関数は一連の命令文を含む。
Figure 4.13

credit: modification of “Tcp connect” by Sébastien Koechlin/Wikimedia Commons, CC BY 3.0 · CC BY 3.0 · Source出典

Figure 4.13 This diagram demonstrates various functions used by the socket on both the client and the server side to establish and close a connection. (credit: modification of “Tcp connect” by Sébastien Koechlin/Wikimedia Commons, CC BY 3.0)
図4.13 クライアント側とサーバー側の両方で、ソケットの接続確立と切断に使う、さまざまな関数を示す。(クレジット:Sébastien Koechlin/Wikimedia Commonsによる「Tcp connect」を改変、CC BY 3.0)
A procedural programming language provides syntax for defining procedures but cannot force individual programmers to follow through with breaking their code up into small procedures and giving the procedures descriptive names. So, even when a program is written in a procedural language, the source code may not necessarily be written in a procedural style.
英語のヒント
手続き型プログラミング言語は、手続きを定義する構文を提供する。しかし、各プログラマーが、コードを小さな手続きに分け、内容を示す名前を付けることを、最後まで徹底するよう強制はできない。そのため、手続き型言語で書かれたプログラムでも、ソースコードが必ず手続き型の書き方になっているとは限らない。