cs101 Lecture4
cs101 Lecture4
32 bits
int sides;
Results in a memory location of size 32 bits being reserved for this
variable. The program will refer to it by the name sides
Variable Names: Identifiers
Sequence of one or more letters, digits and the underscore
“_” character
• Should not begin with a digit
• Some words such as int cannot be used as variable
names. Reserved by C++ for its own use
• Case matters. ABC and abc are distinct identifiers
Examples:
• Valid indentifiers: sides, telephone_number, x, x123,
third_cousin
• Invalid identifiers: #sides, 3rd_cousin, third cousin
Recommendation: use meaningful names, describing the
purpose for which the variable will be used
Some Other Data Types Of C++
• unsigned int : Used for storing integers which will always be
positive
− 1 word (32 bits) will be allocated
− Ordinary binary representation will be used
• char : Used for storing characters or small integers
− 1 byte will be allocated
− ASCII code of characters is stored
• float : Used for storing real numbers
− 1 word will be allocated
− IEEE FP representation, 8 bits exponent, 24 bits significand
• double : Used for storing real numbers
− 2 words will be allocated
− IEEE FP representation, 11 bits exponent, 53 bits significand
Variable Declarations
the value stored in the variable at the time of int i=0, result;
its creation
• If you type the character ‘f’, the ASCII cin >> command;
value of ‘f’ will get stored
Reading Values Into Variables (2)
Some rules:
• User expected to type in values consistent with the type of
the variable into which it is to be read
• Whitespaces (i.e. space characters, tabs, newlines) typed by
the user are ignored.
• newline/enter key must be pressed after values are typed
Printing Variables On The Screen
• General form: cout << variable; cout << x;
• Many values can be printed one after
another
• To print newline, use endl cout << x << y;
• Additional text can be printed by
enclosing it in quotes
• This one prints the text Position: , cout <<“Position:" <<
then x and y with a comma between
them and a newline after them x << “, “ << y <<
• If you print a char variable, then the endl;
content is interpreted as an ASCII
code, and the corresponding
character is printed.
char var = ‘G’;
G will be printed.
cout << var;
An Assignment Statement
Used to store results of computation into a variable. Form:
variable_name = expression;
Example:
s = u*t + 0.5 * a * t * t;
Expression : can specify a formula involving constants or
variables, almost as in mathematics
• If variables are specified, their values are used.
• operators must be written explicitly
• multiplication, division have higher precedence than
addition, subtraction
• multiplication, division have same precedence
• addition, subtraction have same precedence
• operators of same precedence will be evaluated left to
right.
• Parentheses can be used with usual meaning
Examples
• if varA, varB have the same data type: the result will have
same data type
• if varA, varB have different data types: the result will have
more expressive data type
• int/short/unsigned int are less expressive than float/double
• shorter types are less expressive than longer types
Rules for storing numbers of one
type into variable of another type
• C++ does the “best possible”.
int x; float y;
x = 2.5;
y = 123456789;
• x will become 2, since it can hold only
integers. Fractional part is dropped.
• 123456789 cannot be precisely represented
in 24 bits, so something like 1.234567 e 8 will
get stored.
Integer Division
• FIRST evaluate the RHS and THEN store the result into the LHS
variable
• So 1 is added to 12, the value of p
• The result, 13, is then stored in p
• Thus p finally becomes 13
p = p + 1 is nonsensical in mathematics
“=” in C++ is different from “=” in mathematics
Repeat And Reassignment
main_program{
int i=1;
repeat(10){
cout << i << endl;
i = i + 1;
}
}
Sequence generation
• Can you make i take values 1, 3, 5, 7, …?
• Can you make i take values 1, 2, 4, 8, 16, …?
• Both can be done by making slight modifications to
previous program.
Composing The Two Idioms
main_program{
int n, nfac=1, i=1;
cin >> n; Accummulation idiom
repeat(n){
nfac = nfac * i;
i = i + 1; Sequence idiom
}
cout << nfac << endl;
}
Finding Remainder
• x % y computes the remainder of dividing x by y
• Both x and y must be integer expressions
• Example
Example
x += z; // x becomes x+z = 12