by freeCodeCamp.org
We often use IDE (integrated development environment) - a place to write, run and debug codes e.g. VS Code
IDEs are good for:
A syntax is set of rules specific to programming languages -> similar to grammar
Error occurs when syntax is incorrect, and IDE will tell you where error is and won’t run..
Enter your code and use console to keep track of your progress -> a text interface interpreting output of a programme
Console is also seem as a developer tool , not usually meant to be used and interacted by users
Computer can do simple stuff i.e.
String is text, anything enclosed by quotation marks. i.e. "4" while 4 w/out quotation mark is an integer
Concatenation is when adding strings together
Python example: print("game over, " + 4 + " was your final score." )
Good practice to add space at start/ end of your string
Variables store information that can be referenced and manipulated
Each variable has a type, name, and piece of information stored inside
integers, booleans, floats, doubles, strings and chars
Variables are useful to keep track of things e.g. user name or score; taking inputs, making variability and have it changed based on certain factors, manipulate for many tasks
Create a space in memory that stores your variable name and its contents.
Blank variable is for storing info down the road e.g. maybe from user input
Variables can point to the same info/ memory storage
Assign new content to existing variables can update it e.g. RPG games stats
After the code run its course, variables are deleted in memory
#1 Rule for variables is they must be one continuous string
good practice is camelCase - not capitalise first word, but first letter of all words after
Adds variability to program based on user inputs, or else program will run the same way every time
If statement -> if true do this, otherwise do sth else e.g. braces () if true inside braces will run, else will not run
Else if statement -> only when if or (if else) is bypassed due to it being false
Else statement -> carry out after if/ else-if statement is not true
Switch statement -> input a variable then determine which "case" it is Start with a colon (:)
A list of something, e.g. integerts, strings or other arrays, all in an array info is related
It's critical how we reference each element inside of array, computer uses indexes which begins at 0.
When initiate array the type must be determined (String array, integer, double etc), they all have to be the same
Putting array inside an array is 2D array, similar to matrices in math/ physics 2D array use 2 numbers, that is (row, column)
a Loop is a statement to run certain instruction repeatedly
For Loop
infinite loop - you must give a initial integer and operation at some point will be met.
For each loop - interacting w/ entire array or lists
go through each element and carry out instructions for each value
While loop - continue to carry out instruction while a conditional statement is true, can be an infinite loop
e.g. while (x == 0)
Do-while loops - instruction inside the loop will run once before checking conditional statement
Everyday occurance so dont be suprised, consists of three types:
when the computer is given some condition w/ no feasible wayt o finish, result in error and crash
think about ur code flow (i.e. loops)before running i
test ur code often and incrementally
When error occur, read the error coz IDE will print error message FYI.
Trace to the line of code and try to fix.
Go to stackoverflow etc.
Print statement debugging - insert print to validate value from algorithm before enter into potential error prone instructions.
Just remember to delete after checking.
Breakpoint bedugging - pauses the programme when reaches ur breakpoint
Now, to resolve the bug
w/ section of code causing the error, try to commenting it out
Mark-up the code so computer wouldn't read it, test the programme again if see if this section is the issue.
Prevent Errors
you can pull previous versions or backtracking when error was written
a Function is a segment of code that can be run by calling the function name, and it may do something in return.
Function can be called numerous times and in numerous places
e.g. print function - specific code has ben abstracted down to a single line.
Function can be used to recycle sections of code, for equations to allow multiple inputs, and save space.
Thousands of types of functions available u just need to import the ones u need.
There are 4 types of functions, seperated by
A. Whether or not they take in arguments
B. Whether or not they return values
Arguments are variables we pass into a function for manipulation and then either:
e.g. go to cafe to order food (ur function) - you want nachos and latte (ur argument);
e.g. 2 max function needs numbers as input, error occur when input strings
Without Arguments
combines instructions for ease of eye and workload
Function can either return variables or not - returns a string, integer, array etc
Returning function - must be return into something, usually a variable (and printed)
Not return function - takes no arguments and returns no value
The Merits of Function
Allow making large changes easily, as each function call is just a copy of that function code, change the function will change all function calls
Import pre-made functions to reduce workload.
Library - collection of functions that all have same theme. e.g. mech lib, data analysis lib.
An import statement is used to import library, package, class
Import entire library is a waste of computing power + increase load time
python from math import factorial
Java import Jave.math.factorial
A skeleton system for function that works
public
, return type public void
, function name public void printStats
particular to Javapublic void printStates()
e.g. in Python def printStats():
Requires variables from user to pass into the function, the variables will then be used in arguments when we call function
e.g. public void producCalculator(in num1, num2) {
system.out.println(num1* num2);
}
producCalculator(5,8)
returns 5x8 = 40.
when call a function you must pass in its designated variable types
`public ini exampleFunction(ini num1, String str1) {
if (num1 == 5) {
return 5;
} else {
return 10;
}
}
Arraylist is A growing array which dynamically changes its size, starts w/ initial size of 10.
When you don't know the exact number of values to store e.g. database of users
Dictionary stores multiple values and each value is tied to an identifier to reference it, called its "key"
Searching algorithms - ways to look through a list of values stored in a array and find particular piece of data.
Lists can be either sorted (by order or reason) or unsorted (the opposite)
Efficiency based on two values:
Linear search - start from beginning and systematically check each data point until you find what you are looking for.
worst case - check every element 0(n) average case - find our search term halfway 0(n/2)
work with both sorted and unsorted lists
Binary search - a recursive process that break the list down into smaller and smaller parts to find item faster, takes advantage of sorted lists
worst case - runs in logarithmic time 0(log n)
average case - find our item in logarithmic time 0 (log n)
Recursion is using functions that repeated call themselves , in instruction that occur w/in a function, one instruction will be a call to that same function e.g.
public ini myFunction(ini n)
{
instructions;
myFunctions(n+1);
}
public ini recursiveSum(ini n) {
if (n<=1) {
return n;
} else {
return (n + recursiveSum(n-1))
}
}
the above essentially calculates when N = 3, returns 3+2+1 = 6.
The Stack is a data structure containing all tasks the program is instructed to complete, based on a certain method the program will carry out the tasks you give it
LIFO Structure (last in first out) mean the last item put one the stack will be the first removed from it.
if a recursive function is created w/out a reachable base case, processes will continue adding up the stack, causing a stack overflow error and program crash.
Recursion is useful because it breaks down large problem into smaller pieces to compute
Most programmer think about code more often than actually writing it. Simply roll-up ur sleeve and write code doesn't guarantee good program.
Pseudocode simply means plan our the code before writing.
avoid being bogged down w/ syntax and naming conventions, simply making note of what the program ultimate goal should be
Programming languages are built to optimise different tasks. e.g. HTML and CSS - web development
Scripting language has many commands for you run w/out being complied, easier to port between operating systems. e.g. php, javascript
General purpose languages has wide variety of use case and applications