2.2. Values¶
A value is one of the fundamental things—like a letter or a number—that
a program manipulates. The only values we have manipulated so far are
the string values we have been outputting, like “Hello, world.”. You
(and the compiler) can identify string
values because they are enclosed
in double-quotation marks.
There many different kinds of values, called types. This includes integers and characters. An integer is a whole number like 1 or 17. You can output integer values the same way you output strings:
cout << 17 << '\n';
A character value is a letter or digit or punctuation mark enclosed in single quotes, like ’a’ or ’5’. You can output character values the same way:
cout << '}' << endl;
This example outputs a single close curly-brace on a line by itself.
It is easy to confuse different types of values, like “5”, ’5’ and 5, but if you pay attention to the punctuation, it should be clear that the first is a string, the second is a character and the third is an integer. The reason this distinction is important should become clear soon.
Q-1: A value is a single letter, number, or punctuation enclosed in single quotes.
int main() { int x = 7; char c = '8'; while (x < 10) { cout << c << endl; x++; } c = '9'; cout << "It's the year 3000!"; cout << "Just kidding, it's " << 2020 << "!"; }
int main() { char init1 = 'R'; string init2 = "M"; cout << init1 << "+" << init2 << endl; string init3 = "R"; char init4 = 'P'; cout << init3 << '+' << init4 << endl; cout << "Carved their initials in a tree!"; }
-
Q-4: Match the value to its data type.
Try again!
- 1
- integer
- "1"
- string
- '1'
- character
More to Explore
From cppreference.com
The type property