Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/hitowomusubu/www/wp/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340
文(ステートメント) | 夢を叶える塾
スポンサーリンク

文(ステートメント)

ホームページのリニューアルに伴い一部のページは新しいサイトにリダイレクトされます。

 

アルゴリズムは、「演算」、「入出力」、「条件分岐」、「反復」等の組み合わせから作ります。
これらのプログラムの構成要素を文(ステートメント)と言います。
既に簡単な解説をしていますが、この文についてもう少し詳しく解説をしていきます。

演算

基本的な四則演算について解説は不要でしょう。
敢えて加えると言えば、指数の計算には”^”ハットを使います。
3^2で9になります。
また、ルート等は3^(1/2)でも得られますが、ルートを求める関数を使う方法もあります。
と言っても、わざわざルートを計算するための関数をプログラマが作る必要はありません。
プログラム言語によりますが「sqrt(3)」等とすると「1.7320508…」というルート3の値を得る事ができます。
関数については前回解説をしましたが、プログラム言語が元々関数をいくつか備えています。
このほかに説明を加えておくべき演算があります。
条件式と論理演算子です。

条件式

主に条件分岐の際に使います。
数字あてゲームの中で

  1. 正解の数を決める
  2. 正解の数と入力された数が等しければ3へ、そうでなければ5へ
  3. 「セイカイ」と表示する
  4. ・・・

と言った条件分岐の話をしました。
「正解の数と入力された数が等しければ」という命題の真偽を判定する式を条件式と言います。

「正解の数」を変数「seikai」、「入力された数」を変数「korekana」としましょう。
これが等しいかどうか「seikai = korekana」という条件式で判断する事ができます。
もし「seikai」が「21」で「korekana」も「21」であればこの条件式は「true」という「真」を表す値となります。
もし「seikai」が「21」で「korekana」が「24」であればこの条件式は「false」という「偽」を表す値となります。

「hantei = seikai = korekana」は「「seikai = korekana」の条件式の判断結果を「hantei」に代入する」という意味になります。
もし「seikai」が「21」で「korekana」も「21」であれば、「hantei」は「true」になります。
もし「seikai」が「21」で「korekana」が「24」であれば。「hantei」は「false」になります。

条件式には統合以外に「<」や「>」も使うことができます。
「<=」と書けばこれは「以下」の判定で使い、「以上」は「>=」になります。
「hantei = seikai > korekana」の具体例で見てみます。
もし「seikai」が「21」で「korekana」が「20」であれば、これは正しい不等式なので「hantei」は「true」になります。
もし「seikai」が「21」で「korekana」が「24」であれば。これは誤った不等式なので「hantei」は「false」になります。
もし「seikai」が「21」で「korekana」が「21」の場合も、誤った不等式なので「hantei」は「false」になります。

論理演算子

論理演算子とは高校数学で学ぶ「かつ」や「または」等の話になります。

かつ、または

条件式によって得られる真偽に対し、二つの条件式を「かつ」や「または」で結合します。

  1. 真かつ真の結合は真
  2. 真かつ偽の結合は偽
  3. 偽かつ偽の結合は偽

になります。
「かつ」で結合した条件式は両者が真のときのみ真になります。
「かつ」は「and」で表現されます。

  1. 真または真の結合は真
  2. 真または偽の結合は真
  3. 偽または偽の結合は偽

になります。
「または」で結合した条件式は両者のうちいずれかが真であれば真になります。
片方だけ真でも真ですし、両者が真でも真です。
「かつ」は「or」で表現されます。

「1=1 and 2=2」この条件式の結合は、「1=1」が真、「2=2」も真を「かつ」で結合しています。
従って両者共に「真」なので「真」になります。
「1=1 and 2=2」この条件式の結合は、「1=1」が真、「2=2」も真を「または」で結合しています。
いずれか一方が「真」なので「真」になります。

「1=2 and 1+1=2」この条件式の結合は、「1=2」が偽、「1+1=2」が真を「かつ」で結合しています。
両者共に「真」とならないなので「偽」になります。
「1=2 or 1+1=2」この条件式の結合は、「1=2」が偽、「1+1=2」が真を「または」で結合しています。
いずれか一方が「真」となっているので「真」になります。

変数を使っても論理演算子は有効です。
「a=1=1」、「b=1>2」とすると、「a」は「true」、「b」は「false」になります。
「a and b」は「true」と「false」ですから「false」です。
「a or b」は「true」と「false」ですから「true」です。

否定

もう一つ重要な論理演算子があります。
「否定」です。
否定は、「true」を「false」に、「false」を「true」に変換します。
「否定」は「not」で表します。

「a=1=1」とすると「a」は「true」ですが、「not a」は「false」です。
「not 1=2 and 3>2」は「1=2」が「false」で「not 1=2」が「true」、「3>2」が「true」ですから「true」です。

括弧

論理演算子は数学と同じように「()」の括弧で計算の順序を優先させることができます。

「not ( 1=2 and 3>2 )」は「1=2」が「false」で、「3>2」が「true」ですから「1=2 and 3>2」は「false」で、「not (1=2 and 3>2)」は「true」です。

入出力

標準出力、標準入力

入出力と言ったとき、基本的には「標準出力」への出力と、「標準入力」への入力を言います。
プログラム言語により、入出力先が決まっていて、そこからプログラムに値を入力したり、プログラムの結果を出力したりします。
これは基本的にテキストベース(文字のみの入出力)になります。

とはいえ、例えばファイルを指定して何か処理したい場合がありますね。
このようなときは「ファイルパス」を入力し、そのファイルパスにあるファイルに対してプログラムから操作を行うという流れになります。

その他の入出力

標準出力や標準入力というもの以外で入出力を行う事もできます。
プログラムにファイル名を記述しておけば、そのファイルを参照し、ファイル内のデータを参照したり、ファイルに結果を書き込んだりすることができます。
その他にも、GUIベースで入力や出力するためのウィンドウ等を表示し、そこでプログラムと入出力を行うという場合もあります。

先の入出力に比べるとプログラムのインターフェースがわかりやすいため、使い勝手は上がります。
標準入出力はプログラム言語に用意された最も簡単な入出力のため、ソースコードも単純です。
標準入出力を使わない場合、そのインターフェース部分を作成するためのプログラムが必要になります。

条件分岐

条件分岐といっても色々なパターンがあります。
いずれの場合も条件式を用いて、その値によって判断する事となります。

まずは単純な「true」のときのみ処理を行うという分岐です。
if_t

このとき命令Aのステートメントを記載する場所を「then節」と呼んだりします。

次に「false」のときのみ処理を行うという分岐もあります。
if_tf

このとき命令Bのステートメントを記載する場所を「else節」と呼んだりします。

更にこれを組合せ、入れ子(ネスト)にし、3以上の分岐にする事もできます。
if_ttf

この例では「else節」に更に条件分岐を作り、その「then節」に命令文B、「else節」に命令文Cを記述しています。

また、このように「true」と「false」の分岐以外にも、「○○のとき」という形で分岐させることもできます。
if_abc

青、黄、赤、それぞれのときのステートメントをそれぞれ対応する箇所に記述します。

なお、青のとき「進め」、黄色のとき「止まれ」、赤のとき「止まれ」等と書くこともできます。
これは条件文一つで「青かどうか」判断し、「then節」に「進め」、「else節」に「止まれ」でも同じ話になります。
ソースコードの可読性として、青、黄、赤、それぞれどうするかわかりやすくなるメリットがあります。
ただし、冗長ですし、できるだけシンプルな方がわかりやすいという人もいます。
ある程度好みが出てきます。

条件分岐は上記程度の単純な分岐であればよいのですが、難しい判断をしようと思うと複雑になっていきます。
何重にも入れ子が続く事もありますが、色々とよろしくない事が出てきます。
単純に見にくいという話で、ソースコードの可読性が悪化する事があります。
また、プログラムを作ればそれが意図したとおりに動作するか確認するのですが、複雑な分岐全てを想定通りに振り分け出来ているか確認するのも困難です。
できれば入れ子構造が少なく、条件式も簡潔なプログラムを書きたいものです。

反復(ループ)

反復(ループ)は繰り返し同じ処理を行うときに有効な手段です。
ただ、プログラムが終わらなくなってしまわないように、普通はその繰り返す条件を指定します。
この条件の付け方で、反復にもいろいろなパターンがあります。

まずは条件式を繰り返す前に確認するパターンです。
for

反復はハンバーガーで挟んだような形で表します。
このパターンは、条件式が満たされたらこの反復の先(下のバンズの先)のステートメント進みます。
この反復の先に進むことを「ループを抜ける」等と言います。
条件式を確認してから反復内のステートメントを実行します。
そのため、最初から条件式を満たしている場合、反復内の命令Aは実行されません。

この方法とは異なり、処理を行った後に条件式を確認するパターンもあります。
while

この場合、命令Aが必ず一度は実行される事となります。
うまく使い分けないと意図した動作になりませんので注意が必要です。

また、条件分岐同様、反復も入れ子が使えます。
forfor

こちらも条件分岐の場合と同じように、入れ子が複雑になりがちです。
また、反復の入れ子が続くと、演算数が膨大になり、プログラムの実行時間は一気に増大します。
1つの入れ子でn回、2つの入れ子でn^2回、3つの入れ子でn^3回の演算数になるので注意しましょう。

その他

プログラム言語の特色が現れない、共通のステートメントを説明してきました。
ここまでのステートメント以外にも様々なステートメントが存在します。

「0で除算」等によるエラーが発生した時のためのステートメントがあります。
このようなエラー時の処理をエラーハンドリングなどと言ったりします。
「1秒待つ」というステートメントがあります。
色々なステートメントが存在しています。

また、言語によって特殊なステートメントが用意されていたりします。
例えばプログラミング言語VBAにはエクセルのセルの値を変更するステートメントがあります。
※まあ、これはどちらかというと「メソッド」という概念になると思いますが・・・

このようなステートメントは挙げ出せばきりが無いのでここでは割愛します。
ただ、そのようなステートメントが本来やりたい事を実現するためには必要不可欠になるでしょう。

終りに

実は今回で連載としては最後になります。
サンプルコード等が無いため、プログラミングを学んでいる感覚があまりなかったかもしれません。
それだけになおさら「え?もう終わり?」と思われるかもしれません。
ですが、プログラミングの基礎としては、ここまでで大部分が終わっています。

この後は実際のプログラミング言語を用いて学んで頂けたらと思います。
私も「VBA」の連載の方を始めたいと思いますので、そちらを読み進めて頂ければと思います。

また、プログラミングとしても、まだまだ記事は作成していく予定です。
ステートメントをどのように組み合わせるとやりたいことができるようになるのかの解説がありません。
そのサンプルを掲載していきたいと思っています。

ここまでお読みいただきありがとうございました!
プログラミングの学びに少しでもお役に立っていれば光栄です!

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

にほんブログ村 地域生活(街) 東北ブログへ にほんブログ村 地域生活(街) 東北ブログ 米沢情報へ にほんブログ村 教育ブログへ にほんブログ村 教育ブログ 塾教育へ
スポンサーリンク