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
What C++ library must be used when using math functions?
<cmath> : defines many mathematical functions such as sqrt (square root) and pow (raising to a power)
Should look like #include <cmath>
</cmath></cmath>
What are constants?
Constants are values that cannot be changed
How can constants be distinguished?
They are typically written in capital letters to be distinguished
EX: there will always be 365 days in a year, so we may use const int YEAR = 365;
What are magic numbers and why should they be avoided?
Magic numbers are a numeric constant that appears in your code without explanation. Avoiding them limit confusion.
What are string literals?
String literals are a sequence of characters enclosed in quotation marks.
EX: cout «_space;“I love programming!”;
*notice how the quotations appear on the same line.”
What C++ library must be used when dealing with strings
We must use <string>. Don't forget to include using namespace std;</string>
What are string variables?
String variables are variables that store string literals. String literals are a sequence of characters that are enclosed within quotation marks.
*Strings can hold letters, numbers, spaces, and symbols (periods, commas, exclamation, question mark, addition, etc)
Explain the difference between strings and chars
Strings can hold a sequence of characters while chars can only hold one character. You can tell the difference by looking at the quotation marks. Strings will always have the double quotation (“ “), while chars will always have the single quotations. (‘ ‘)
What is concatenation?
Concatenation is a form of string manipulation. We use the + operator to add different strings together.
EX:
{ string s1, s2, s3, s4;
s1 = “Hello “;
s2 = “every”;
s3= “one!”;
s4= s1+ s2+ s3;
coud «_space;s4 «_space;endl;
return 0; }
Output: Hello everyone!