Back • Return Home
Introduction To Programming & Computer Science
These are some notes based on the video, Introduction To Programming & Computer Science by NullPointer Exception and freeCodeCamp. We aren't going to cover everything given within the video, just highlight a few important concepts. The whole video is worth watching though.
Useful Analogies
• Memory is like a warehouse full of storage containers.
• Initializing a Variable is like putting a piece of information within a storage container.
• A "Memory Address" is where a storage container is located within the warehouse.
Types of Conditional Statements
All of these allow our code to branch off into different possibilities by comparing whether something is "True" or "False".
• if Statement - "if something is True, then do this..."
• if-else Statement - "...if it is something else (i.e.: False), do this other thing instead."
• else-if Statement - "if some part is True, do that. else skip to the rest of the program."
• else Statement - "run when all previous statements are False"
• switch Statement - This holds multiple Conditional Statements so that we can switch between them.
Types of Loops
All of these allow bits of code to run repeatedly.
• for Loop - "run for as long as [blank] is True"
Important: In order to keep it from making an "Infinite Loop" that the computer cannot handle, we have to specify a requirement that will eventually be satisfied (the "Base Case").
• for each Loop - "repeat for each value in a list"
• while Loop - "move on with the rest of the program, but continue to do this while [blank] is True"
Some while Loops are "infinite" in that they only end when you exit out of the program.
Arrays & Dictionaries
An "Array" is like a Variable that holds more than a single piece of information. However, all of these pieces must all be of the same type (e.g.: integers). An Array of Arrays is like a table.
An "Array List" is essentially an Array that can have more Memory allocated to it when more information is put into it. (Normally, Arrays have a set size.)
A "Dictionary" can hold multiple values like an Array, but they are not referenced linearly one after another in Memory. Instead, each value has a unique "key" that identifies it.
Types of Errors & How To Do Simple Debugging
• Syntax Error - code that does not follow the structure of the language used
• Runtime Error - when the computer cannot run code in a reasonable time (e.g.: because of an Infinite Loop)
• Logic Error - the result is different from what is wanted, but there are no Syntax or Runtime Errors
To avoid Syntax Errors, one has to get more comfortable with using the language. An IDE can help, but we can also try typing in programs that we are studying so that its use becomes a habit.
To avoid Runtime Errors, check the Conditional Statments and Loops. Use else Statements at the end of code to catch any unforeseen conditions.
To avoid Logic Errors, repeatedly test out code as you write it. This will help you to catch exactly when something is not working properly.
Use print Statements to find out where in the code the unexpected results are being produced. Be sure to erase them when they are no longer needed.
Another approach is to use "Breakpoints". All code will be run until it reaches the "Breakpoint".
Do both of these approaches together: Use print Statements to check the content of Variables, and use Breakpoints to slowly comb through the code. Once we've found the offending piece of code, we should NOT delete it immediately because it was probably put there for a reason. Instead, comment it out and test again. We might need to rewrite some of the code.
Types of Functions
In the context of programming, "Functions" are a bunch of instructions that have been simplified into a single line of Higher-Level Language.
An "Argument" is a piece of data that must be given in order for a process to be done. For example, we must state the exact food items that we want when we order at a restaurant. A "Return Value" is when something is given in response to an instruction.
There are four types of Functions:
1. Take In Arguments and Return A Value
2. Take In Arguments but Do Not Return A Value
3. Do Not Take In Arguments but Return A Value
4. Do Not Take In Arguments and Do Not Return A Value
Libraries
"Import Statements" can be used to bring in code that other people have already written to save time. These pieces of code are usually organized by theme into "Libraries". Whenever we import, we have to specify the Library, the "Package" inside of the Library that we need, and what "Class" from the Package that we are using.
We should try to be specific and not import an entire Library unless we have to. It takes up more computing power to do so.
Recursion
"Recursion" is when a Function calls itself repeatedly. It can make some tasks easier to code.
A "Stack" is a Data Structure that contains all of the tasks your program will complete. It is called a Stack, because if another process is started before another is completed, it is "stacked" on top of the previous one (Last In, First Out).
A "Stack Overflow" is when processes are continually added to the stack without any of them ever being completed. It is a type of Infinite Loop.
Search Algorithms
"Search Algorithms" are ways to look through a Data Structure to find some particular piece of data.
"Sorted Lists" have values that are ranked. "Unsorted Lists" have no pattern. Some Search Algorithms may work for one, the other, or both. A couple of examples...
• Linear Search
A Linear Search starts at the beginning of a list and goes through each data point in order until it finds what we are looking for. The worst case scenario is that what we are looking for is the last thing on the list. On average, we would find what we are searching for halfway through the list. It is slow, but it works on both Sorted and Unsorted Lists.
• Binary Search
A Binary Search recursively breaks down a Sorted List into smaller parts until it finds what we are looking for.
For example, check the middle entry of an alphabetical list. If it doesn't match, check the first letter. If what we are looking for starts with a letter that comes before that in the alphabet, we can ignore everything after that entry because we know that what we are looking for won't be contained within it. We can then repeat the process on this new shorter list.
By looking at the entries that are right next to the one that is being checked, we can determine if we can ignore portions of the list that are not relevant. This helps us to narrow down our pool of options.
Binary Search is way faster than a Linear Search, but only applies to Sorted Lists!
Always Plan Ahead
"Pseudocode" is an outline, a simplified explanation of what it is supposed to do. It ignores details (like Syntax and Naming Conventions) to get the basic logic down. What must be done? Once you have a plan, then you can figure out how to carry it out. Some useful tools are:
• Flowchart - a graphic representation of a Function
• Write-Up - a list of what you want done and in what order, a set of tasks organized chronologically
• Functionality Planning - a list of features that we want the user to have and what Functions we will need to do them
Which Language?
It depends on what we want to do. For example:
"Scripting Languages" have many commands and can be run without being compiled. They make "Scripts" that are usually faster to write and easier to port between Operating Systems. [Examples: Perl, PHP, Ajax, Javascript]
"General Purpose Langauges" can be used for many different applications, but may also have some task that they are usually used for. [Examples: Java for Game/Web Development, Python for Data Analysis, C++ for System Programs]