[go: up one dir, main page]

0% found this document useful (0 votes)
9 views2 pages

Week 2 Review Variables and Assignments

Uploaded by

k61ca.2242219031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Week 2 Review Variables and Assignments

Uploaded by

k61ca.2242219031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Monday

● Labor day.

Wednesday
● A variable declaration is a statement that declares a new variable, specifying the
variable's name and type. Ex: int userAge;
○ The compiler allocates a memory location for userAge capable of storing an
integer. Allocation is the process of determining a suitable memory location to
store data like variables. The happens behind the scenes when the compiler
compiles the code for int userAge;

● Identifiers must:
○ Not be a reserved word. A reserved word is a word that is part of the language,
like int, short, or double. A reserved word is also known as a keyword. Table
2.3.1 in the zyBook shows a list of reserved words in C++.
○ Be a sequence of letters (a-z, A-Z), underscores (_), and digits (0-9).
○ Start with a letter or underscore.

● An assignment statement assigns the variable on the left-side of the = with the current
value of the right-side expression. Ex: numApples = 8; assigns numApples with the value
of the right-side expression (in this case 8).

● An expression may be a number like 80, a variable name like numApples, or a simple
calculation like numApples + 1. Simple calculations can involve standard math operators
like +, -, and *, and parentheses as in 2 * (numApples - 1). An integer like 80 appearing
in an expression is known as an integer literal.

● Although not required, an integer variable is often assigned an initial value when
declared. Ex: int maxScore = 100;

● A common error is to read a variable that has not yet been assigned a value. If a
variable is declared but not initialized, the variable's memory location contains some
unknown value, commonly but not always 0. A program with an uninitialized variable
may thus run correctly on a system that has 0 in the memory location, but then fail on a
different system—a very difficult bug to fix. A programmer must ensure that a program
assigns a variable with a value before reading.
Friday
● Integer Division:
○ Description: When you divide one integer by another, the result is also an
integer. This means any fractional part is discarded (or, more technically,
truncated).
○ Example: 7 / 3 in integer division yields 2.
● Floating Point Division:
○ Description: When you divide one floating-point number by another, the result
retains its fractional part.
○ Example: 7.0 / 3.0 yields 2.3333... (depending on precision).
● When working with division in programming, it's crucial to be aware of the types of the
operands and the type of the variable you're assigning the result to. Implicit conversions
can lead to unintended truncation of results or loss of precision.

● Use int for whole numbers when precision and speed are crucial.

● Use double for real numbers, especially when you need to handle fractions or very
large/small numbers.

You might also like