Variables, Types and Expressions 2.1 Identifiers: A More Complete List
Variables, Types and Expressions 2.1 Identifiers: A More Complete List
return 0;
Program 2.2.1
return 0;
}
Program 2.2.2
which produces the output:
The character
decimal or 20 hex.
The character
decimal or 21 hex.
...
...
The character
decimal or 7D hex.
The character
decimal or 7E hex.
float number;
Square Root
1
2
3
4
5
6
7
8
9
10
1.00
1.41
1.73
2.00
2.24
2.45
2.65
2.83
3.00
3.16
int
int
int
int
int
int
int
MON = 0;
TUES = 1;
WED = 2;
THURS = 3;
FRI = 4;
SAT = 5;
SUN = 6;
Example:
Equivalent to:
number += 1;
number = number + 1;
total -= discount;
bonus *= 2;
bonus = bonus * 2;
time /= rush_factor;
change %= 100;
amount *= count1 +
count2;
count2);
The first of the above examples may written in even shorter form.
Using the increment operator "++", we may simply write
number++;
The operator "++" may also be used as a prefix operator:
++number;
but care must be taken, since in some contexts the prefix and
postfix modes of use have different effects. For example, the
program fragment
x = 4;
y = x++;
results in "x" having the value 5 and "y" having the value 4,
whereas
x = 4;
y = ++x;
results in both variables having value 5. This is because "++x"
increments the value of "x" before its value is used, whereas "x+
+" increments the value afterwards. There is also an operator
Expression:
True or False:
false
(6 <= 6) || (5 < 3)
true
(5 != 6)
true
true
false
true
65)
...
...
if (total_test_score >= 50 && total_test_score <
test.\n";
...
...
...
...
if (number_of_people = 1)
cout << "There is only one person.\n";
...
...
will always result in the message "There is only one person"
being output to the screen, even if the previous value of the
variable "number_of_people" was not 1.
(BACK TO COURSE CONTENTS)
2.6 Summary
In this lecture we have discussed variables in more detail. We
have seen how variables are always of a particular data type, and
have listed different ways in which variables may be temporarily
or permanently assigned values. We have also seen how new
values of various types can be generated by the use of operators.
The material here is also covered in more detail in Savitch,
Chapter 2, and Sections 4.2 and 4.5.
Exercises