









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A series of slides from the university of virginia (uva) cs120 course, covering topics such as variable initialization, output, input, types, and arithmetic operators. It explains the concept of setting variables before using them, the use of the insertion operator for output, and the difference between integer and real types. It also discusses the rules for numerical literals and character variables, as well as type compatibilities and mixed arithmetic.
Typology: Study notes
1 / 16
This page cannot be seen from the preview
Don't miss anything!










cout << "Hello, World" ; cout << "Enter the Number:\n" ;
Syntax cout << exp 1 << exp 2 << ... << exp (^) n;
Note
Semantics
Syntax cin >> var 1 >> var 2 >> ... >> var (^) n ;
Semantics
Examples cin >> fahrenheit; cin >> x_coordinate >> y_coordinate; BETTER: Issue a prompt for input. cout << "Current temperature? "; cin >> fahrenheit;
Literals and variables have types
Basic types of variables:
Why so many?
What to do?
Beware!
Common sense, for the most part
Integers ( int , long , ..)
Reals ( float , double ...)
Remember:
General rule: you cannot store a value of one type in a variable of a different type --- type mismatch - convert if possible int distance, rate; distance = 33.9; - stores 33 rate = 2.0; - stores 2
Textbook error: bottom of page 59 - what he says is wrong. One can always assign a real value to an integer variable - it will always be truncated
Need to recognize the very distinct difference between division of two real values and the division of two integer values.
Remember the rule: in A op B if any of the operands are real, the result is real. Putting this another way, the integer operand value is first converted to an equivalent real value, and then the opera- tion (+, -, *, or /) is performed.
so, if we have 9.0/2.0, we obviously get 4.50000 as the result
if we have 9.0/2, then 2 is converted to 2.0000, gives 4.
however, if we have: 9/2, then neither operand is real, so we have to do INTEGER DIVISION --- result is 4 (with a remainder of 1)
The operator ’%’ (called "modulus") yields the remainder after division:
9/4 has value 2 (with remainder of 1) 9%4 has value 1 (the remainder after 9/4)
10/4 has value 2 10%4 has value 2
11/4 has value 2 11%4 has value 3 Trick question: value of val*(1/4)?
-11%4 has value -3.........
double price; price = 78.5; cout << "The price is $" ; cout << price << endl;
Output The price is $78. The price is $78.500000 WHICH????? The price is $7.8500000e
Output we want The price is $78.
Magic formula (textbook p.51) cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Output The price is $78.
Common
Note
Precedence Rules