Chapter 2 Flashcards
What is a variable? What are the different types?
A variable is a place to store information. There are integers (int) floating point numbers, strings, etc.
What are assignment statements and what do they do?
They store the value of an expression in a variable and replaces the previously stored value. When you set a variable equal to something, that is called an initialization.
EX: variable = statement;
What factors are and aren’t allowed when naming a variable?
- Variables are case sensitive
- Variables should have descriptive names
Cans: - numbers
- letters
- underscores_
Cannot:
- symbols (!, $, %)
- spaces
- reserved words (int, double, include)
- start with a number
Examples on naming variables: legal or illegal?
- Day_Of_Week
- 3dGraph
- _employee_num
- June1997
- MixedUp#3
- Day_Of_Week // legal
- 3dGraph // illegal
- _employee_num // legal, not recommended
- June1997 // legal
- MixedUp#3 // illegal
What are the different data types we deal with?
- Number data types (int, double, etc)
- Characters
- Booleans
What’s the difference between integers and doubles?
Integers are whole numbers while doubles are decimal numbers
What does the boolean type mean?
True or false
What are character types used for?
Make sure to use single quotations to denote
- They are used to store a single character such as ‘X’ or ‘$’
Why do overflow errors occur?
An overflow error is when the number is too large and the numeric type can’t store it.
Why do underflow errors occur?
Usually happens for floating types like doubles
- They occur when the result is too small to be represented in the type being returned by the function.
What is casting?
Casting is a conversion between different data types.
It is better to use explicit casting rather than implicit casting bc info can be easily lost
Explicit Casting Format: static_cast<data> ()</data>
EX: int n = static_cast<int> (10.9);
- n now holds 10</int>
What is an roundoff error?
Roundoff errors happen bc some numbers can’t be precisely represented with the binary
A trick to solve this is to +0.5
How is arithmetic evaluated in C
++?
They are evaluated with the same rules in mathematics. Follow PEMDAS!
Compare post increment vs. pre increment. (x++ vs. ++x)
Post increment is when the incrementation occurs after the command, while pre incrementation occurs before the command. The same goes for pre/post decrementation
Division vs. Mod (%)
Division gives us the number after dividing two numbers. % gives us the remainder after dividing two integers. If the remainder is 0, that means both integers are even.
If the numerator is less than the denominator, the % is equal to the numerator