markdown
プログラミングの基本md bd150c5
lecture/information/programming/introduction-to-programming.lecture.n.md
Download PDF

プログラミングの基本きほん

date2026-07-14document_iddoc_f0e88254a7ce6fe5b40abf46ade12832descriptionプログラムを入力から出力を生成する手続として捉え、状態、制御フロー、分岐、反復、関数の基本を説明する。prerequisites四則演算 / 条件の読み取り / 手順の分解type講義statusactiverelateddata/lecture/information/information-engineering-portal.lecture.n.md / data/lecture/information/programming-languages/foundation/introduction-to-functional-programming.lecture.n.md / data/lecture/information/algorithm/foundation/computational-complexity-basics.lecture.n.md / data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md
informationprogrammingundergraduatelecture
data/lecture/information/information-engineering-portal.lecture.n.md

1導入どうにゅう

この講義こうぎでは、プログラムを、入力にゅうりょくを受うけ取とって出力しゅつりょくを生成せいせいする手続てつづきとして説明せつめいする。その実行過程じっこうかていを状態じょうたいと制御せいぎょフローによって記述きじゅつし、変数へんすう、条件分岐じょうけんぶんき、反復はんぷく、関数かんすうの役割やくわりを解説かいせつする。

2基本用語きほんようご

入力にゅうりょくInputとは、プログラムが計算けいさんに使用しようするために外部がいぶから受うけ取とるデータである。出力しゅつりょくOutputとは、プログラムが計算結果けいさんけっかとして外部がいぶへ提示ていじするデータである。画面がめんへの表示ひょうじ、ファイルの読書よみかき、ネットワーク通信つうしんは入出力にゅうしゅつりょくInputoutput; IOの例れいである。

状態じょうたいStateとは、ある実行時点じっこうじてんにプログラムが保持ほじする値あたいの総体そうたいである。変数へんすうVariableとは、プログラムから値あたいを参照さんしょうするための名前なまえである。代入だいにゅうを許ゆるす変数へんすうでは、対応たいおうする値あたいを更新こうしんできる。

制御せいぎょフローControl flowとは、プログラムの各命令かくめいれいを実行じっこうする順序じゅんじょである。条件分岐じょうけんぶんきConditional branchは条件じょうけんの真偽しんぎに応おうじて次つぎの処理しょりを選択せんたくし、反復はんぷくIterationは指定していした条件じょうけんの下もとで処理しょりを反復はんぷくする。

関数かんすうFunctionとは、引数ひきすうとして入力値にゅうりょくちを受うけ取とり、一連いちれんの処理しょりを実行じっこうして返値かえりちを返かえす、名前なまえを付つけたプログラム構成要素こうせいようそである。言語げんごによっては引数ひきすうまたは返値かえりちを持もたない関数かんすうも存在そんざいする。

3実行じっこうを状態遷移じょうたいせんいとして記述きじゅつする

状態じょうたいを S、一ひとつの命令めいれいを c とすると、命令めいれいの実行じっこうを

(c,S)⟶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 を再評価さいひょうかする。反復はんぷくの設計せっけいでは、各回かくかいで更新こうしんする状態じょうたい、継続条件けいぞくじょうけん、終了時しゅうりょうじに成立せいりつすべき性質せいしつを区別くべつする。

text
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 inputInput is data received from outside a program for use in a computation. An outputOutput is data presented outside the program as a computational result. Screen display, file access, and network communication are examples of inputoutputInputoutput; IO.

A stateState is the collection of values held by a program at a particular point during execution. A variableVariable is a name through which a program refers to a value. If a variable permits assignment, its associated value can be updated.

Control flowControl flow is the order in which the instructions of a program are executed. A conditional branchConditional branch selects the next operation according to the truth value of a condition, while iterationIteration repeats an operation under a specified condition.

A functionFunction 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)⟶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.

text
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
raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる