Skip to content

Instantly share code, notes, and snippets.

@Kev-in123
Last active July 1, 2023 02:41
Show Gist options
  • Save Kev-in123/5a316a2c82cfb361bb989674c98f6b1d to your computer and use it in GitHub Desktop.
Save Kev-in123/5a316a2c82cfb361bb989674c98f6b1d to your computer and use it in GitHub Desktop.
Test: Selection & Repetition
Data types (probably useless but since there is a coding portion, I'm including them anyways):
boolean - stores true or false
byte - stores whole numbers from -128 to 127
char - stores a single character/ASCII values
int - stores whole numbers from -2,147,483,648 to 2,147,483,647
long - stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
short - stores whole numbers from -32,768 to 32,767
float - stores fractional numbers, sufficient for storing 6 to 7 decimal digits
double - stores fractional numbers, sufficient for storing 15 decimal digits
Catch-Exception:
try - tries to execute some code
catch - catches the exception raised in try (if applicable)
finally - executes no matter if the try-except failed or passed
throw - throw an error
throws - what error may be thrown
Naming conventions (probably useless but since there is a coding portion, I'm including them anyways):
1) under 256 characters
2) only letters, digits and underscores (_)
3) can’t start with a digit
4) no spaces
5) no reserved keywords (refer to Quiz 1 notes I sent a while ago)
Logical Operators:
|| - or
&& - and
! - not
Comparison Operators:
== - equals to (does not apply to strings; however, you can use these to compare chars)
!= - not equals to (does not apply to strings; however, you can use these to compare chars)
> - greater than (does not apply to strings; however, you can use these to compare chars)
< - less than (does not apply to strings; however, you can use these to compare chars)
<= - less than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars)
>= - greater than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars)
Arithmetic Operators:
+ adds the numbers (also used to concatenate strings to strings/numbers/chars)
- subtracts the numbers
* multiplies the numbers
/ divides the numbers (if both numbers used are both integers, drop the decimal once done dividing)
% divides the numbers and returns the remainder
++ increments the number by 1
-- decrements the number by 1
Assignment Operators:
a = b;
- assigns b to a
a += b; - same as a = a + b;
- can be used to increment a number by b
- or to concatenate strings
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value)
a -= b; - same as a = a - b;
- can be used to decrement a number by b
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value)
a *= b; - same as a = a * b
- can be used to multiply a number by b
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value)
a /= b; - same as a = a / b
- can be used to divide a number by b
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value)
a %= b; - same as a = a % b
- can be used to divide a number by b and return the remainder
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value)
- indexes start from 0 and end at 1 less than the length
- in a for loop, you would do
String s = "Hello, World!";
for (int i = 0; i < s.length(); i++) {
...
}
// only goes up to 1 less than the length of s (no equals sign)
switch - The expression is compared with the values of each case, if none match, then the default is used, if there isn't a default, nothing happens
case - Used in a switch-case statement to specify a case
default - The default block of code in a switch statement, if it's the last statement, it doesn't need a break
break - exits the loop/switch-case
continue - stops the current iteration of the loop and moves on to the next
if - used to check a condition, if true execute the code once
else if - if the condition in the if statement is false and the condition passed is true, execute the code
else - if the condition in the if/else-if statement(s) is false, the code is executed
while - similar to if, however, continually executes the code until the boolean is false
do - used together with while to create a do-while loop (please don't do this, just use a while loop normally)
for - typically used to perform a task a specific number of times or be used to loop through an array (in python we say list instead of array)
Notes on switch-case statements:
- The variable used in a switch statement can only be an integer/byte/short/char/strings/enums
- You can have any number of case statements within a switch
- The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached
- When a break statement is reached, the switch terminates
- Not every case needs to contain a break. If no break appears, it will fall through ALL the remaining cases until a break is reached
Notes on if/else-if/else statements:
- If braces aren't used, only the first expression is executed
- If all the statements are ifs and no else-ifs are used, the expression will fall through all of the ifs. Use else-if if you only want one to be checked
- Don't nest if statements if not necessary (example below)
assume a, b, c, and d are pre-defined boolean values (true/false values don't really matter in this example)
don't do:
if (a && b) {
if (c && d) {
//code here
}
}
do:
if ((a && b) && (c && d)) {
//code here
}
Another example
don't do:
if (a || b) {
if (c || d) {
//code here
}
}
do:
if ((a || b) && (c || d)) {
//code here
}
for loop
- loops a fixed number of times
e.g.
for (int i = 0; //executed (only the first iteration) before the execution of the code block. Typically a variable is defined here but that doesn't stop you from calling a function.
i < 10; //the condition for executing the code block (checked on every iteration). Must be a boolean.
i++ //executed (on every iteration) after the code block has been executed. Typically a variable is incremented here but that doesn't stop you from calling a function.
) {
... //code after
}
more redable:
for (int i = 0; i < 10; i++) {
... //code after
}
- Highly recommended (by me):
if you are iterating through a string/array and its length never changes,
you can initialize a variable as the length to avoid calling the `length` function on every iteration
same applies to while and do-while loops
for-each loop ("enhanced for loop")
- used to iterate over an iterable
e.g.
int nums[] = {0, 1, 2, 3, 4, 5};
for (int i: //variable declartion
nums //iterable
) {
... //code after
}
more readable:
int nums[] = {0, 1, 2, 3, 4, 5};
for (int i: nums) {
... //code after
}
while loop
- the boolean statement is first checked
- everything in the loop is executed continously until the boolean statement becomes false
e.g.
int i = 0;
while (i < 10) {
i++; //if i isnt incremented, the loop will run forever
... //code after
}
You can also increment after the block is done running
but that makes the difference between starting from 1 and ending at n and starting from 0 and ending at n-1 where n is the number checked in the boolean statement
int i = 0;
while (i < 10) {
... //code before
i++; //if i isnt incremented, the loop will run forever
}
do-while loop
- everything in the loop is executed at least once
- the boolean statement is checked before executing again
- note the semicolon after the while
e.g.
int i = 0;
do {
i++; //increment
... //code after
} while (i < 10);
Likewise in the while loop, you can run the code before incrementing
but that makes the difference between starting from 1 and ending at n and starting from 0 and ending at n-1 where n is the number checked in the boolean statement
int i = 0;
do {
... //code before
i++; //increment
} while (i < 10);
A bunch of laws on booleans (we probably won't need them):
Associative Laws
(A && B) && C = A && (B && C)
(A || B) || C = A || (B || C)
Distributive Laws
A && (B || C) = (A && B) || (A && C)
A || (B && C) = (A || B) && (A || C)
Complement Laws
(A && !A) = false
(A || !A) = true
Identity Laws
(A && true) = A
(A && false) = false
(A || false) = A
(A || true) = true
De Morgan's laws
!(A || B) = !A && !B
!(A && B) = !A || !B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment