1/42
Saving Future Husband Part 2

When does a Compile error happen?
Before the program runs

When does a Runtime error happen?
While the program runs

When does a Warning happen?
At compile time, but it still builds

What does int code hold?
Whole numbers

What does float code hold?
Decimals (Unity's default)
Note: The f matters. 5.5 is a double. float speed = 5.5; won't compile — write 5.5f

What does double code hold?
Decimals, more precise (C#'s default)

What does bool code hold?
True or false

What does char code hold?
One character, single quotes

What does string code hold?
Text, double quotes
What's the difference between a compile error and a runtime error?
A compile error stops the program from building; a runtime error happens while it runs.
Why won't float speed = 2.5; compile?
2.5 is a double; a float literal needs 2.5f.
What does 17 % 5 evaluate to?
2.
int total = 10; int count = 4; float avg = total / count; — what is avg, and how do you fix it?
2.0, because 10 / 4 is integer division. Fix: float avg = (float)total / count; → 2.5

What is this an example of?
if / else if / else — checked top to bottom. The first true condition wins, and the rest are skipped.
Note: Order matters. If you checked health < 25 first, a player with 0 health would print "Critical" — that condition is also true, and it came first.

What is this an example of?
Combining Conditions.

What is this an example of?
Switch — choose one path from many possible values of one variable:
Note: Each case ends with break. default runs when nothing else matches. (break inside a switch is the one place course standards allow it.)
In an if / else if / else chain, how many blocks can run?
Exactly one.
What's wrong with checking score > 50 before score > 90 when assigning grades?
A score of 95 matches > 50 first and never reaches the > 90 check.

What is this an example of?
for — when you know how many times
Note: Three parts: start (int i = 0) · keep going while (i < 5) · after each trip (i++)

What is this an example of?
while — when you don't know how many times. It repeats as long as its condition is true.

What is this an example of?
do-while — runs at least once, then checks.

What is this an example of?
Infinite loops. If the condition never becomes false, the loop never ends — and in Unity, the editor freezes
Note: Every while loop needs something inside it that moves it toward stopping.
Off-by-one errors. i <= 5 runs six times (0 through 5). i < 5 runs five. Be deliberate.
How many times does for (int i = 2; i < 8; i++) run?
6 — for i = 2, 3, 4, 5, 6, 7.
When would you choose while over for?
When you don't know in advance how many times it needs to run.

What is this an example of?
An array holds a fixed number of values of one type.

What is this an example of?
Index from 0. The last index is always Length - 1.
Note: Out of range is a runtime error: scores[5] → IndexOutOfRangeException. There is no index 5 in an array of length 5.

What is this an example of?
Default values: int/float elements start at 0, bool at false, and string or object elements at null — an empty slot.

What is this an example of?
Arrays are reference types. The variable holds an arrow to the array.
An array has Length 8. What are its first and last indexes?
0 and 7.
What does new string[4] contain right after it's created?
Four nulls — slots exist, but nothing is in them.

What is this an example of?
The template: A traversal visits every element of an array, usually in order. Almost every array task is a traversal plus one small job done at each element.
Note: Use i < Length, never i <= Length. The second one reaches an index that doesn't exist.

What is this an example of?
Traversals, Pattern 1 — Visit every element
What is this an example of?
What is this an example of?
What is this an example of?