General Coding Notes
Notes that are about the general concept(s) behind coding/programming.
Programming
Programming is giving a computer a set of instructions to execute.
The mental process of thinking of instructions to give a machine.
Coding
The process of transforming those ideas into a written language the machine can understand.
Many languages use ; to end a line of code.
Variable
Used to assign a name to a piece of data in order to later reference said data by name elsewhere in the program.
Defined by giving a name and setting it to a value using '='.
The value of a variable can be changed as needed.
Data Types
DT's (Data Types) are classifications for different forms of information.
Data is pieces of information used to build programs, such as numbers in an equation, or text printed on screen.
DT's tell us about data, how it can be stored and what it can be used for.
Numbers, Strings, and Booleans are what's called primitives, the most basic data types.
Numbers
The earliest data type.
Represent numerical values, with and without decimals.
Have special properties
Describe
numbers are used to express value: what is the frequency of a sound wave? that can be expressed with a number like 800hz.
Calculate
Numbers are used in calculations: What's the distance between Earth and Mars?
Count
Numbers are used to keep track: How many times did the car go around the track?
String
A sequence of characters surrounded by single or double quotes. Commonly used to represent text, speech, symbols, etc.
Strings can be used for various things:
- Display data that uses text or symbols
- Add or remove text
- Modify characters
- Allow computer to communicate in a 'human-readable' way
a string '20' would be read as two separate characters by the computer, '2' and '0', while 20 contains the numerical value of 20.
Strings can be visualized as chunks of text, but remember that they’re just a collection of individual characters, like letters in a banner.
Boolean
Represent true and false.
Used to:
- Determine validity
- Make decisions
Operators
Operators are different symbols that represent an operation, such as the plus sign '+' as a symbol for addition. Operations let us process our data to transform it into something else.
Arithmetic Operators
They include addition '+', subtraction '-', multiplication '*', and division '/'.
Addition adds to a number: 1 + 1 = 2
Subtraction takes away: 1 - 1 = 0
Multiplication repeats a number: 2 * 2 = 4
Division splits a number up: 10 / 5 = 2
Comparison Operators
Often need to compare value or check if a value is correct. Comparison operators allow us to compare values and evaluate their relationship. They evaluate to Boolean values (true or false). Expressions that evaluate to Boolean values are known as Boolean expressions.
Include:
Less than < value to the left is less than the value to the right
Greater than > value to the left is more than the value to the right
Equals == checks if value to the left is equal to the value to the right (note: a double- equals sign is used to show a value is being checked, not set)
Not equal !== checks if the value to the left is not equal to the value on the right
Less than or equal to <= checks if the value on left is less than OR equal to value on right
Greater than or equal to >= checks if value on left is greater than OR equal to value on the right
Two main instances where comparison operators are used:
If a quantity is unknown
What if we knew that we needed a half pound of strawberries, but we didn’t know the weight of each strawberry? We could weigh the strawberries and see if the total weight equals a half pound.
If we need to compare two known values
If we’re making a salad that’s super citrusy, then we need to make sure we have more oranges than bananas. If that’s false, I’ll have to add more oranges.
Logical Operators
Evaluate multiple Boolean expressions. Rather than determining if one relationship is true or false, it looks at several relationships by connecting them with logical operators and then determining the logic/validity of the overall expression.
AND && - If both expressionss are true, evaluate true otherwise evaluate false.
((4 > 1) AND (2 < 7)) = true same as
(true AND true) = true
OR ||- only one expression needs to be true to evaluate true.
((8 > 6) OR (3 > 6)) = true same as
(true OR false) = true
NOT !- the expression always evaluates to the opposite.
NOT(1 < 3) = false same as
NOT(true) = false
Functions
A function is a named sequence of instructions, packaged as a unit, that performs a specific task. Defined by giving instructions and a name, called by writing it's name. When a function is called all of it's instructions are exectuted.
function doThing(){
instruction1
instruction2
instruction3
instruction4
}
Basically just grouping a bunch of shit together so its
1. more organized,
2. easier to do a set of instructions repeatedly, and
3. easier to change instructions when needed
Control Flow
Each program is a set of instructions, executed in a certain order. This order is called the control flow.
Control structures are used to alter the control flow.
Control structures include conditionals (statements that allow the execution of different blocks of code based on certain conditions).
Conditional
A conditional statement checks for specific conditons, and performs a task based on the conditions.
Programmers can give multiple sets of instructions and describe the right conditions in which to use each set. They can describe a structure like the quoted program above: “if X, then do instruction set 1. Otherwise do instruction set 2.” This structure is called a conditional control structure because the computer’s instructions depend on some condition(s).
Conditionals allow programs to do different things in different scenarios.
Comparison, or logical operators are sometimes used when writing conditional statements.
If Statements
If statements check a condition, and if it's true then code is executed.
if(condition = true){
Run this code
}
If Else Statements
Basically an if statement, but has an extra condition for if the condition is false.
if(condition = true){
Run this code
} else{
Run this code instead
}
Loops
A loop is a sequence of instructions which is specified once but can be carried out multiple times in a row. Loops can also specify that the instructions are repeated until a certain condition is met. Each execution of those instructions is called an iteration
For Loops
Aka count-controlled loops.
Executes a set of instructions for a specified number of times. When a computer receives this program it sets a counter to 0 and executes the instructions in the body of the loop. After each iteration (one pass through the instructions), it advances the counter by 1. The process repeats until the counter is at the set number.
While Loops
Aka condition-controlled loops. Repeats a set of instructions while that condition is true. Computer checks if condition is met every loop, once it's no longer satisfied it stops. Use when you know when a program should stop, but not the number of times it should repeat.
For Each Loops
Aka collection loops.
Define a desired sequence (a list) and tell the computer to repeat the instructions for each item in the sequence. Use this loop when you need to perform a task for every item in a list, or when the order of things must be maintained.
Exception
In exception handling, two sets of instructions are defined. The first set is executed, and if an exception occurs the first set is stopped and the second set of instructions is executed.
Data Structures
Formats that can be used to keep track of data in an organized fashion.
Lists
A very basic data structure. Used as a container to store pieces of information that relate to each other in some way. Lists order data in a specific linear sequence.
Sequence is important because it tells us each specific position a value is in the list. The position of the value is known as its index.
Indices are numbers. Usually an index will start at 0 and go up by one each value.
This allows for the selection of specific values in order to do something with it.
Items can be added and removed from lists. When adding to the end of an existing list it's called appending.
In JavaScript the .push command is used for appending.
.pop is used for removing items from the end of a list.
Items can also be inserted into/removed from existing lists using the index number for where we want to position of the new/removed value.
In JavaScript you'd use the .splice command for this.
You can select different items on a list and store them as a variable.