## The Chernotes #1Your handy study guide and review sheet

VARIABLES AND FUNCTIONS

Variables (13 min - easy)

Memory

• Where are variables stored?

• Every primitive data type has a purpose. What's Cherno's take on this?

• Be able to declare, initialize, and assign variables in C++

• How many bytes are an integer?

• How many bits are in a byte?

• What is a signed integer and why does this affect the size of an integer?

• Approximately what is the range of an int in C++?

• What is an unsigned number?

• How do we make an int be unsigned in C++?

Types

• Know the purpose and size of the following types:
char, short, int, long, float, double, bool

• If I write 5.5 in C++, what type is it automatically treated as?

• How do you make a decimal value be treated as a float?

• What number(s) are true and which number(s) are false?

• Why is a bool one byte instead of one bit?

• How can you tell the size of a data type in your C++ code?

Pointers and References

• How do you make a pointer?

• How do you make a reference?

Functions (10 min - easy)

Basics

• What exactly are functions?

• When a function is in a class, what do we call it?
Note: In Java, everything is in a class.

• Why might you want to use functions?

• Know how to write and call a function including:

• A return type

• One more more parameters

Example of Why Functions Are Important

• What error does Cherno make when copying the code?

• What point is he trying to make?

• How could we avoid this sort of error?

• Be comfortable writing a function like MultiplyAndLog

Limits of Functions

• Why does Cherno warn to be careful not to write a function for "every line of code"?

• What's the "asterisk" that Cherno added to his statement about efficiency?

• What is the primary purpose of functions and how can that help guide you on deciding when to write them?

Return Values and Main

• Do functions with a return type need to return values?

• What is going on with int main() ?

• In C++ we break up functions into declarations and definitions. Where are these stored?
Note: Coming from Java, this seems weird. We'll get there.

Header Files (15 min - medium)

Why?

• You know how we declare variables before we use them in Java? Well, in C++ you need to do this for functions. A header file is a place we store those declarations.

• To summarize the first 2 minutes, a lot of this has to do with how C actually manages function declaration. It's okay if you're not 100% following this yet.

• Watch his example with a log function. You might want to pause and try to replicate this in a Replit project.

• Be able to explain the benefits of a header file

• Practice making a header file in Replit

• What does #include do?

• What does #pragma once mean?

• Why would I include a file twice if I'm not stupid?

• What does #ifndef do and how does the syntax work?

• Why do both exist, and what does Cherno recommend?

• Why do some include statements use quotes and others use angle brackets?

• Why doesn't iostream have an extension?

Conditions and Branches (20 min - medium)

Definition

• What two things happen when we run an if statement?

• Since we jump around to different parts of memory, what is one cost of branching?

If Statement Example

• Be able to replicate this example. A lot of this is the same as Java, so you're expected to know:

• if, else if, and else

• Comparison Operators: == < <= >=

• Logical Operators: && || !

• Note: We won't have all the debugging features in Replit that Cherno is using in Visual Studio. If you're running VS on your own computer and want to learn about it, I'd encourage you to check out his Debugging video.

• Look at the Assembly code in his example - you'll notice that one line of code might be many operations.

• Note: You don't need to follow all the details he goes through on Assembly code, but it's interesting to see how this works behind the scenes.

• What is "constant folding" and why would the compiler remove lines six to eleven if x = 6?

Booleans, Zero, and One

• Why don't we need to do "== true" or "!= 0" in an if statement?

• Does the line if(1) work? How does it work?

• Why does Cherno avoid writing an if statement all on a single line?

• Does the line if(x) work? How does it work?

• Don't stress about pointers. We'll get there in a few days!

Else If and Close

• Make sure you know the difference between if and else if

• Why is an "else if" actually a trick?

• What are the two kinds of programming you'll encounter according to Cherno, and what are they like?

• Cherno's coming from a very efficiency, C++ style. If you like more mathematical stuff and optimization, you may enjoy working on stuff like game engines. If you aren't into it, there's a lot of areas of programming that don't focus as much on that style.

LOOPS AND FLOW

Loops (12 min - easy)

For Loops

• For loops have the exact same syntax as Java. You should be able to write these in your sleep.

• Be ready to explain the three parts of a for loop

• The stuff inside the curly braces is called the ____.

• Look at Cherno's weird example of deconstructing a for loop. This would be very silly to do, but it's worth seeing how things actually work.

• What happens if I run a loop like: for( ; ; )

While Loops

• For loops have the exact same syntax as Java. You should be able to write these while riding a unicycle.

• Be ready to show how to use a while loop

• Why use one over the other?

• For example, consider Cherno's game loop vs. iterating over an array example.

• Be ready to write a do while loop

• These are the same as Java, but you likely did not have much experience writing them. You should be able to write these while sitting down and thinking.

Flow (8 min - easy)

What are the three types of control statements

• What does continue do and where can you use it?

• What does break do and where can you use it?

• What does return do and where can you use it?

Continue

• Watch Cherno's example and be ready to use it in code.

• Play around with the example in Replit.

• Did you forget how modulus works? You should know it. It's the same as Java... if needed, google!

Break

• Watch Cherno's example and be ready to use it in code.

• Play around with the example in Replit.

Return

• Watch Cherno's example and be ready to use it in code.

• Play around with the example in Replit.

POINTERS AND REFERENCES

Pointers (17 min - hard)

Theory

• What does Cherno think is the most important thing in programming?

• What are pointers?

• Describe Cherno's metaphor for memory.

• "Types are just some kind of _______ we've created to ________."

Example

• Why do we often use types with pointers anyway?

• What is a void* ?

• What does the value 0 mean for a pointer?

• What other words can we use for this value?

• What happens if we reference it in our programs?

• What does the ampersand operator do and how do you use it?

• What changes in memory when Cherno switches the void* to an int*?

Dereferencing

• How do I dereference a pointer and what does it mean?

• What happens if we make a mistake and assign the wrong type to a pointer? Will the compiler catch it?

Closure

• Cherno discusses a bit about pointing to an address vs. a block of memory. It's okay if this is fuzzy for you, but I want you to get the main idea out of it.

• Pointers are stored in memory. What does that make possible?

• In a 32 bit application, how big is a pointer?

• Note: This math should make sense to you.

References (10 min - hard)

Theory

• Cherno says that references are just _______ in disguise.

• Cherno has used the phase "syntax sugar" or "syntactic sugar" a few times now. Google it - what does it refer to?

Example

• Follow his example. Be able to declare a reference.

• Cherno creates what he calls an "alias" - will this actually be in the machine code when it is compiled?

• What is the difference between passing by reference and passing by value? How do you designate one over the other in C++?

• Once you declare a reference, can you change what it references?

• Be aware that when you make a reference you must initialize it to an actual variable.