Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
All chapters' source material finished
Advanced Arithmetic & arithmetic are "semi-final" -- unless I see major issues there, they're done. See chapter-status.txt for details on where we are.
- Loading branch information
1 parent
52be944
commit 2cae4b0
Showing
10 changed files
with
550 additions
and
296 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
We call a variable whose value we cannot change a constant. | ||
After you declare a constant, you are unable to change it, no matter what. | ||
There are two types of constants: literal and declared constants (\Code{const}). | ||
|
||
\LevelD{Literals} | ||
|
||
A literal is a value outide of a variable such as $5$, $9$, $103$, and $-21$. Each of those is an \Code{int}, but a literal constant can be of any data type. The point is, these are values that the C++ compiler already recognizes, and can’t be changed. In other words, you can’t convince the compiler to give the literal 3 the value of 4, because 3 is constant. The table below contains a few examples. | ||
|
||
13.8903 | ||
94.2321 | ||
-389283220.342423 | ||
float | ||
‘x’ | ||
‘R’ | ||
‘%’ | ||
char | ||
Be aware, that C++ interprets the difference between a char and a single-character variable name by the enclosure of (‘) single quotation marks. | ||
true | ||
false | ||
bool | ||
Notice that a bool only has two literal values, true or false. | ||
|
||
|
||
Declared Constant | ||
|
||
So what’s the difference between declaring a normal variable and a constant? When we declare a constant, we simply place the keyword const before the data type in the declaration. This indicates that whatever declaration follows the const will be a constant and cannot be changed. Since it’s a constant, you will also need to initialize the value at the same time you declare the variable. Here is an example: | ||
|
||
const float pi = 3.14; | ||
float radius = 5, area; | ||
|
||
area = radius * radius * pi; | ||
cout << area; | ||
|
||
//program outputs 78.5 to the screen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.