COS132 Chapter 3
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
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