新人Y子のプログラミングのお部屋27

論理演算を、Cでプログラミングしてみましょう!

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]今回は久々にC言語に戻ります!

[/speech_bubble]
[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]はい。

[/speech_bubble]
[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]次のCプログラムを見てください。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]このプログラムは、ただ足し算して結果を表示するだけのプログラムです。
処理を説明できますか?
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]はい。
えーっと7〜9行目から、y, a, bという名前の3つの変数を使おうとしてますね!?
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]はい、変数宣言ですね。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]次に、11, 12行目で、aに11を、bに22を代入していますね。
14行目で、変数aとbを足した結果が、変数y に代入してますね。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]はい。その通りです。
そして最後のprintf関数で、足し算の結果が出力されます。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]プログラムの実行結果を、Coding Groundで確認してみてください。
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-normal.png" name="新人Y子"]実行しました!
「計算結果は、33 です」と表示されました。

[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]はい。それでは本題です!
上のプログラムの足し算の部分を、OR演算に変えてみましょう。
[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]OR演算の記号は、以下の表のとおり「|」です。

[/speech_bubble]

[speech_bubble type="std" subtype="L1" icon="itokuma-normal.png" name="しげちゃん先生"]では、このプログラムの実行した結果を、まず考えてみてください。

[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="sanoyu-hatena.png" name="新人Y子"]え〜っと・・・分かりません。
論理演算の入力は、0か1だけではなかったですか?
[/speech_bubble]

[speech_bubble type="std" subtype="R1" icon="techtrageman-normal.png" name="テクトレージマン"]次回に続く[/speech_bubble]

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

目次