COSC-051 Lecture Notes - Lecture 5: Operand, Type Conversion

64 views2 pages

For unlimited access to Class Notes, a Class+ subscription is required.

February 1st, 2017
Assigning a value to a variable:
-assigns a value to a previously created variable
-a single variable name must appear on left side of the = symbol
int size;
size = 5+2; // this works
5 = size; // this does not work
Initializing a variable:
-gives an initial value to a variable at the time it is defined
-some or all of the variables being defined can be initialized
int length = 12;
Using auto (not really important right now):
-if you are initializing a variable when it is defined, the auto keyword will determine the data type to
use based on the initialization value
auto length = 12; //int
auto width = length;
auto area = 100.0; // double
Scope
-the scope of a variable is that part of the program where the variable may be used
-a variable cannot be used before it is defined
Operators:
Everything is normal except
“/“ means division
EX: ans = 7/3;
Output = 2
** Note: The remainder is thrown away, so the output is only 2
“%” is a modulus and it means
ans = 7 % 3
Divide 7 by 3, and the remainder is the answer
Multi-line comments
-begin with /* and end with */
-can span multiple lines
/* — — — — — — — — — —
Here is a multi line comment
__ __ __ __ __ __ __ __ __ __ __ */
-can also be used as single-line comments
Data type conversion and type casting:
-operations are performed between operands of the same type
-if operands do not have the same type, C++ will automatically convert one to be the type of the
other
-this can impact the results of calculations
Hierarchy of data types:
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class
Class+
$8 USD/m
Billed $96 USD annually
Homework Help
Class Notes
Textbook Notes
30 Verified Answers
Study Guides
1 Booster Class