Skip to Content
図解まるわかり プログラミングのしくみ
book

図解まるわかり プログラミングのしくみ

by 増井 敏克
July 2020
Beginner to intermediate
240 pages
6h 17m
Japanese
Shōeisha
Content preview from 図解まるわかり プログラミングのしくみ
72
 プログラムで値を格納する場所を指定する方法に、変数と定数の2つが
あります(図3-9)。
 数学の方程式などでは、求めたい値に使う
𝑥
や
𝑦
といった記号を変数
といいます。これはその値が変わることを意味しますが、プログラミング
の場合も実行中に値が変わるさまざまなデータをメモリ上に格納するとき
に変数を使います。
 何度も複雑な計算をする場合、それらに共通する計算を事前に行ってそ
の計算結果を格納しておけば、処理の中で結果を再利用できて効率的で
す。そこで、途中のデータを保存する場所を確保し、その場所に名前をつ
けます。この名前を指定すると、格納されている値を読み出せます。
 上記の例では値を書き換える必要はありませんが、繰り返して処理をし
たい場合など、値を変えたい場合もあります。掛け算の九九を計算する場
合、1 から9 までの数を全部書くよりも、変数に1 から9 までの値を格納
して変えながら処理すると、プログラムが見やすくなります(図3-10)。
 変数は、格納されているデータを書き換えることができます。つまり、
何度でも値が変わる可能性があり、その中身を見ないと何が格納されてい
るかわかりません。ある開発者が格納した値は、他の処理で書き換えられ
ている可能性があります。これは、プログラムの内容によっては不具合が
発生するリスクが高まることを意味します。
 そこで、一度格納したデータを書き換えられないようにしたものに定数
があります(図3-11)。定数は変数と同じように、同じ値を複数の箇所で
使う場合も、その値を何度も書く必要がありません。定数を使えば、デー ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

図解まるわかり Web技術のしくみ

図解まるわかり Web技術のしくみ

西村 泰洋

Publisher Resources

ISBN: 9784798164212