Logic And Loops
View Code
Basic Logic
- Boolean
- a variable type intended to hold a value of true or false
- TRUE
- a value of something being valid, often represented with a non-zero
number
- FALSE
- a value of something not being valid, often represented with zero
- AND
- a logical "and" statement that is true only when both sides are true
- OR
- logical "or" statement that is true when either side is true
- XOR
- logical "exclusive or" statement that is true only when one side is true
-
- This logical operator is very rare and often expressed in bit
manipulation which most programmers avoid.
- However this is great trivia in academic tests.
- Read its definition carefully and look at the table in the examples.
- You can also think of it as only true when both values are different.
- NOT
- logical "not" statement that makes a true into false, false into true,
AND into OR, and OR into AND
- Truth Table
- a diagram used to represent the total possibilities of a logical
expression
If's and Switches
- if
- a block of coding following the if statement is run when the condition is
true
- else
- paired with an if statement, a block of coding is run when condition is
false
- else if
- a chaining technique of else and if statements for selective cases
- select/switch
- a statement for program flow; it receives a value then runs through the
possible case statement under it, if the value equals a case then the
lines of code after the case are executed
- A switch statement is very much like a else/if ladder but more
eloquently written.
- case
- one situation for the select statement, if the value given is the same as
the case then the following lines are executed
- default
- these lines of code are executed if no case is matched for the value
passed into the switch
Loops
- Definite Loop
- a loop with a specific number of iterations e.g. for
- Indefinite Loop
- a loop with an unknown number of iterations e.g. do/while,
while
- Iteration
- one execution of the block of code for a loop
- do/while
- a loop executed at least once and then when the Boolean value is TRUE
- while
- a loop executed when Boolean value is TRUE
- This loop is very useful for error checking.
- until
- sometimes a language will have loops expressed with until; they work just
the same but now the loop stops when the Boolean value is TRUE
- for
- a loop executed during a counting of a variable; the loop is given a
starting value, ending value, and amount to increment or de-increment
often "i" is the variable
- This loop is very useful for processing arrays.
"Jumping"
- break
- a statement that breaks flow of program out of one block (i.e.
brace couple) of code in a if/else, switch statement
- It is not recommended to use a break statement with a if/else or loop
block of code since that block can always be written without the break
- It is recommend to use a break with a switch statement since they
cannot be written with the same logic without them.
- continue
- a statement brings the flow of program back to the condition check in the
block
- This also should be treated like a break statement.
Prev --
Back to Portal --
Next