Last active
March 27, 2023 22:54
-
-
Save Kev-in123/8d06adc4f70c2f5fbc9e3e33f1c27980 to your computer and use it in GitHub Desktop.
Quiz: Selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|| - or | |
&& - and | |
! - not | |
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 | |
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 the cases until a break is reached | |
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 | |
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 | |
} | |
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