0% found this document useful (0 votes)
2 views

CodingTips

Uploaded by

Ashborn
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CodingTips

Uploaded by

Ashborn
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

IDE's.

· We use IDE's ( Integrated Development Enviromnents) to write a code. A place to write, run, and
debug code and also convert it to machine code.
· IDE's are like any other program on your computer except used for facilitation of code. Examples
( NetBeans, IntellJ, VisualStudios).
· In addition to a place code, IDE's also include
1- Buil in Error- checking ( For when code doesn't go right).
2- Auto-fill for frequently used words.
3- Project Hierarchy.
· A big step up from previously used methods of programming.
Syntax.
· Rules you must follow to a tee if you want your program to run correctly.
1- How you type out certain functions.
2- What you put at the end of each line of the code.
3- How you set up certain functions.
· Syntax for each programming languages is unique.
· Breaking programming rules will result in an error.
· As example. Let's initialize a variable in Java, Python, and JavaScript.
1- In Java, we must specify which type of variable we are defining, and also add a semicolon at the end
of the line. int variable = 3;
2- In Python, we just type what we want to creat. x = 3
3- In JavaScript, we specify we are making a variable, but don't define what type of variable. var x = 3
· The goal of the program was the same, but all three languages shown look different approaches.
· If ypu forget one semicolon or misplace a character, the entire program will not run and send
you a syntax error.
The console.
· Programmers keep track of their progress by looking at the console.
- A text interface whithin the computer tha us programmers can use for a variety of
purposes.
· The main use of the console is to output text from the program using code, more specifically a
print statement.
- Prints text to the console for the programmers viewing pleasure.
· To use the print statement, simply instruct the console to print, and then whatever you want to
be printed inside the parenthesis.
- Using Python, we can print to the console like so. print("Hello World")
· Print statement is also used for viewing and interpreting the computer's out put from a program.
- Example: computing 4+3 will print nothing to the console.
Printing 4+3 however, will print 7 to the console.
Modulus.
· Most programming languages has an additional operator known as modulus.
- Represented with %.
· Allows us to get the remainder a divisional operation.
· When we take 10 moduls 3...
- We essentially tell the computer to devide 10 by 3, ignore the answer, and give us the
reminder --> 1. print(10 % 3)
· In the case wher there isnt't a remainder..
- The computer will simply print/return 0. print(50 % 2)

· This is extremly useful when determinimg if a number is even or odd.


- If a number modulo 2 is 0 -> the number is even.
- If a number modulo 2 is 1 -> the number is odd.
Strings.
· Strings are another way of saying text.
- "Hello World"
- "A"
- Anything enclosed by quotation marks is denoted as a String.
- Example: Game over, 4 was your final score.
-> print("Game over," + 4 + "was your finale score.")
· 4 in quotation marks ("4") is treated as a STRING.
· 4 without qoutation marks (4) is treated as an INTEGER.
Variables.
· Something that can store information.
- Can be referenced and manipulated.
· Each Variablehas a type, a name, and a piece of information stored inside of it.
- Name is simply a name for the variable.
· Many types of variables, but we will be focusing on primitive type variables.
- Integers, booleans, floats, doubles, strings, and char's.
· Integer's.
- A variable that can store and Integer value. -2147483648 to 2147483648.
- CAN'T and WILL NOT hold any decimal values.
· Booleans.
- Can store a value of either true or false.
- Can ONLY hold true or false (no other type of information).

· Floats and Doubles.


- Both are types of floating point data types. Can store numbers with decimal places.
- Float Variables. Can store up to 32 bits of information.
- Double Variables. Can store up to 64 of information.
· Strings.
- What we talked about before, except stored somewhere in a variable.
- Useful for displaying text and storing input information.
- Also useful for outputting information in a readable format for the user.
· Char's.
- Char -> Character.
- Each hold one character.
- Useful when a programmer wants to read one button press of one character in a string
without using a string variable. Example: game controlled by keyboard.
- TIP: You can store char's in a string variable, but not strings longer than 1 character in a
char variable.
· Often times you're going to want to keep track of things such as a user's name or score. By
creating a variable you can store this information in chat variable and then reference, add to, or
modify it.
· Other important uses for variables
- Taking input from the user
- Making your program have variability, have it change on certain factors.
- Manipulating variables is necessary for many tasks in programming.
Conditional Statements.
· The most basic conditional statement is the IF statement
- If something is true do this, otherwise do something else.
· Most programming languages us barce ().
- Whatever is inside the braces will be evaluated as either true or false.
· If the statement is true, whatever is enclosed inside a sat of curly barces directly after the if
statement will run.
· The Else-if statement will only be ecaluated if the precending if (or if-else) statement was
bypassed to it being false.
· Check the initial statement
- If it's true we run that segment of code and move on with the program.
· If it's not true.
- We move on to any else if statements and evaluate those conditional statements.
· If any of them are true.
- Run that segment of code and move on.
· The else statement comes after an if/if-else statement and will always carry out instructions.
- Carries out instructions if all previous cases are not true.
· An easily collapsible way to write many if-else statements.
- You input a variable, then determine which "Cases" that variable could be.
· Start with a colon (:)
- Each switch statemnt also includes a 'default" case (Else Statement).
· Adds variability to programming.
- Program runs differently based on user input.
· If a user does something, we want to be able to adapt accordinly.
· Without, a program would run the same way every time.
Array's.
· Variables are very good at storing singular bits of information.
· As a result, they are unable to hold more than one piece of data.
· An array is a list of something.
- Can be Integers.
- Can be Strings.
- Can even be other array's.
· All information in an array is related.
· Like columns is Google Sheets.
· The single most important thing to note about array's is how reference element inside of them.
- In programming we use Indexes.
1. That numbers "place" in the array.
2. Indexing for arrays begind at 0.
· If you slip up and accidentally reference the 10th elements in this array.
- Would result in an "Out of Bounds" error since you are actually trying to reference the 9th
element.
· Populate First.
- Insert the elements you would like in the array immediately.
· Populate Later.
- Create an array with a specific size, but choose to add elements later.
· When we creat array their sizes are FINAL.
- Cannot be increased or decreased in size through conventional methods.
- This what allows us to easily access their indexes.
· We CANNOT change the size of an array within the code.
· When initializing an array, you must determine its type then and there.
- Example ( String array, Integer array, Double array, etc).
· They have to all be the same type.
· Putting an array inside an array is known as 2-Dimensional Array.
- Similar to Matrices in math/physics classes.
· Each index in the array is actually another array with it's own set of indexes.
· To index 2D array's we use 2 numbers.
- First number is the row.
- Second number is the column.
Loop's.
· A Loop is a statement that is used to run certain instructions repeatedly.
· Very useful for repeated sections of code.
· If we wanted to print something 15 times..
- We could use 15 print statements.
· Or we could use a loop.
- Allows us to repeat parts of code multiple times.
· The For Loop.
- Used when you would like to carry out a certain set of instructions numerous times.
· Consists of 3 parts.
- An integer value.
- A conditional statement the integer must reach to exist the loop.
- An operation to modify the integer value after the instructions inside of the loop are
completed.
· When using a for loop, you MUST make sure you set up a condition that, given the initial integer
value and the operation, will at some point be met.
- Otherwise an infinite loop will occur.
· The For Each Loop.
- Used for interating through entire arrays or lists.
· The llop will go through each element in the array and carry out a set of instructions for each
value.
· Useful for performing operations across an entire collection of data.
· The While Loop.
- Will continually carry out its instructions while a cinditional statement given it is true.
· Similar to a for loop, but broken apart.
· Can sometimes be used to purposely crate an infinite loop.
· The Do-While Loop.
- Functionally similar to while loops.
- However, will always carry out instructions AT LEAST ONCE.
· Instructions inside loop will run once before checking the conditional statement.
· Benefits of Loops.
- Perform operations many times in a row.
- Able to iterate through arrays and lists.
- Decrease clutter of your code.
Errors.
· Code doesn't always work as expected.
· These are known as errors.
- Come up often in computer science.
· Three different types.
- Syntax Errors.
- Runtime Errors.
- Logic Errors.
· Syntax Errors.
- Parts in your program where you fail to meet the programming rules, resultimg in an error.
· Usually the easiest of the 3 to solve.
· Highlighted by the IDE in most cases.
· IDE's underline syntax errors and usually provide helpful hints.
· Syntax Errors are like small misspellings or grammatical errors.
· Some IDE's will restrict you from running the code unless all syntax errors are cleared.
· Runtime Errors.
- Don't appear until you actually "run" the code.
- Caused by a part of your code not being able to be computed in a reasonable amount of
time.
- Most common form -> The Infinite Loop.
· Essentially what happens with the computer.
- It is given some condition with no feasible way to finish that task.
· Puts the computer in error mode.
· Will never reach the ending condition, causing a crash.
· Avoiding runtime errors.
- Think through the flow of your code before running it ( especially loops ).
- Carefully plan out yoyr code before writing -> Pseudocode.
· Logic Errors.
- The code runs smoothly without runtime or syntax errors, but the result isn't what you
wanted.
· Often the hardest type of errors to solve.
- Most of the time, the error is unkown to the programmer.
· One strategy is coding incrementally.
- Test your application often so that if you mess up you know where the error is.
Debug.
· First step is to read the error.
- IDE wil often print out an error message to the console.
· Traverse to the line of code provided by the error.
· Use online forums such as StackOverflow.
· When syntax or runtime error pops up, you should be able to find a fix it fairly easily.
· Breakpoints.
- Pauses the program when the line you placed the breakpoint at is reached until you continue.
· When you think you've found the section of code causing the error.
- First try commenting it out.
· Commeting.
- Allows us to mark-up the code without the computer reading it as actual code.
- Essentially a documentation tool for programmers.
· Backup code frequently.
· Version mangers such as GitHub or SubVersion can help.
- Backup code to an online cloud service in which you can easily oull previous versions of the
program.
· Useful for backtracking to find when the error was written.
· Run your program frequently.
- Prevents you from saving a backup that doesn't work.
- Makes it easier to figure out when you wrote the error.
Functions.
·

You might also like