COS 132
CHAPTER 3 – EXPRESSIONS AND INTERACTIVITY
UNIT 3.1- THE CIN OBJECT
The cin object can be used to read data typed at the keyboard.
cout << “What is the length of the rectange? ”; // This is know as a prompt
cin >> length;
The “>>” symbol is the stream extraction operator, it gets the characters from the stream object onits left and stores
them in the variable whose name appears on the right. The << and >> operators point in the direction that the data is
flowing.
The cin object causes a program to wait until data is typed at the keyboard and the enter key is pressed. Cin
automatically converts the inputted data to the data type of the variable.
NB – you must include <iostream> header file in any program that uses cin.
ENTERING MULTIPLE VALUES
cin >> length >> width;
The enter key is pressed after the last number is entered. Cin will also read multiple values of different data types.
When the user types a value, those first values are stored in an area memory known as the keyboard buffer.
UNIT 3.2 – MATHEMATICAL EXPRESSIONS
C++ allows you to construct complex mathematical expressions using multiple operators and grouping symbols.
Result = 4;
Result = ;
Result = a + b + c;
1|P a g e
, COS132 Chapter 3
OPERATOR PRECEDENCE
Highest - (unary negation)
* / %
Lowest + -
ASSOCIATIVITY
An operators associativity is either to the left or the right, if two operators sharing an operand have the same
precedence, they work according to their associativity.
- (unary negation) Right to left
* / % Left to right
+ - Left to right
GROUPING WITH PARENTHESES
This forces some operations to be performed before others.
NO EXPONENTS PLEASE!
Raising a number to a power requires the use of a library function, called pow.
area = pow(4.0, 2.0);
This statement contains a call to the pow function, the numbers inside the brackets are called arguments (= data being
sent to the function). The pow function raises the first argument to the power of the second argument. The result is
returned from the function and used in the statement where the function call appears.
The program must include <cmath> header file, and the arguments that you pass to the pow function should be doubles.
The variable used to store pow’s return value should also be double.
UNIT 3.3 – WHEN YOU MIX APPLES AND ORANGES: TYPE CONVERSION
When an operator’s operands are of different data types, C++ will automatically convert them to the same data type(this
is known as type coercion). When a value is converted to a higher data type it is promoted, and when it is converted to a
lower data type, it is demoted.
2|P a g e
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller jennamortonx. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $5.07. You're not tied to anything after your purchase.