プログラミングの基本
informationprogrammingundergraduatelecture
data/lecture/information/information-engineering-portal.lecture.n.md
1導入
この講義では、プログラムを、入力を受け取って出力を生成する手続として説明する。その実行過程を状態と制御フローによって記述し、変数、条件分岐、反復、関数の役割を解説する。
2基本用語
入力とは、プログラムが計算に使用するために外部から受け取るデータである。出力とは、プログラムが計算結果として外部へ提示するデータである。画面への表示、ファイルの読書き、ネットワーク通信は入出力の例である。
状態とは、ある実行時点にプログラムが保持する値の総体である。変数とは、プログラムから値を参照するための名前である。代入を許す変数では、対応する値を更新できる。
制御フローとは、プログラムの各命令を実行する順序である。条件分岐は条件の真偽に応じて次の処理を選択し、反復は指定した条件の下で処理を反復する。
関数とは、引数として入力値を受け取り、一連の処理を実行して返値を返す、名前を付けたプログラム構成要素である。言語によっては引数または返値を持たない関数も存在する。
3実行を状態遷移として記述する
状態を S、一つの命令を c とすると、命令の実行を
(c,S)\longrightarrow S'
という状態遷移として表現できる。たとえば、状態 S で x=2 のとき、代入 x := x + 1 は x=3 である新しい状態 S' を生成する。この := は更新を表し、数学の等号とは異なる。
4分岐と反復
if p then A else B は、条件 p が真なら A、偽なら B を実行する。while p do A は、p が真である間、A を実行してから p を再評価する。反復の設計では、各回で更新する状態、継続条件、終了時に成立すべき性質を区別する。
total := 0
for each score in scores:
total := total + score
return total
この例では、scores が入力、total が更新される状態の一部、return total が出力を決定する処理である。n 個の点数を一度ずつ処理するため、加算回数は n 回である。この評価は後続の計算量の講義で形式化する。
5関数による分割
関数には、処理の目的、許容する入力、返値、状態や入出力への影響を仕様として付与できる。大規模な手続を責務の明確な関数へ分割すると、各部分を独立に検証しやすくなる。
ただし、すべての関数が入力値だけで返値を決定するとは限らない。共有状態の更新や入出力を実行する関数もある。この相違は次の関数型プログラミングの講義で詳述する。
6要点
- プログラムの振舞いは、入力、出力、状態、制御フローを区別すると明確になる。
- 条件分岐は処理を選択し、反復は処理を再実行する。
- 関数は処理を仕様の明確な単位へ分割する。
7次に進む講義
data/lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
data/lecture/information/algorithm/foundation/computational-complexity-basics.lecture.n.md
Programming Fundamentals
data/lecture/information/information-engineering-portal.lecture.n.md
1Introduction
This lecture presents a program as a procedure that receives input and produces output. It describes execution in terms of state and control flow and explains the roles of variables, conditional branches, iteration, and functions.
2Basic Terms
An input is data received from outside a program for use in a computation. An output is data presented outside the program as a computational result. Screen display, file access, and network communication are examples of input.
A state is the collection of values held by a program at a particular point during execution. A variable is a name through which a program refers to a value. If a variable permits assignment, its associated value can be updated.
Control flow is the order in which the instructions of a program are executed. A conditional branch selects the next operation according to the truth value of a condition, while iteration repeats an operation under a specified condition.
A function is a named program component that receives input values as arguments, executes a sequence of operations, and returns a return value. Some languages also permit functions with no arguments or no return value.
3Describing Execution as State Transitions
If S is a state and c is one instruction, execution can be represented as a state transition
(c,S)\longrightarrow S'.
For example, if x=2 in state S, the assignment x := x + 1 produces a new state S' in which x=3. Here := denotes an update and is distinct from mathematical equality.
4Branching and Iteration
if p then A else B executes A when condition p is true and B when it is false. while p do A executes A while p is true and then reevaluates p. Designing an iteration requires distinguishing the state updated on each iteration, the continuation condition, and the property that must hold at termination.
total := 0
for each score in scores:
total := total + score
return total
In this example, scores is the input, total is part of the updated state, and return total determines the output. Processing each of n scores once performs n additions. A subsequent lecture on computational complexity formalizes this analysis.
5Decomposition into Functions
A function can have a specification that states its purpose, accepted inputs, return value, and effects on state or I/O. Decomposing a large procedure into functions with clear responsibilities makes each component easier to verify independently.
However, not every function determines its return value solely from its input values. Functions may update shared state or perform I/O. The next lecture on functional programming examines this distinction in detail.
6Summary
- A program's behavior becomes clearer when its input, output, state, and control flow are distinguished.
- A conditional branch selects an operation, whereas iteration repeats an operation.
- Functions decompose a computation into units with explicit specifications.
7Next Lectures
data/lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md
data/lecture/information/algorithm/foundation/computational-complexity-basics.lecture.n.md