ap csp - pseudocode conditons, booleans, logic quiz review

variables

var name = "variable"
var score = randomNumber(1,10)
score = score + 1
  • always use var

  • strings use quotes

  • can’t start names w/ numbers


conditionals

if (score >= 90) {
  setText("gradeLabel","A")
} else if (score >= 80) {
  setText("gradeLabel","B")
} else {
  setText("gradeLabel","C or below")
}

comparison ops:
== equal != not equal > < >= <=
logical ops:
&& and || or ! not

fix reminders:

  • = assigns, == compares

  • no ; after if()

  • match all {} and ()


boolean logic
| a | b | a && b | a || b | !a |
|---|---|---------|--------|----|
| T | T | T | T | F |
| T | F | F | T | F |
| F | T | F | T | T |
| F | F | F | F | T |

pseudocode:

  • IF (A AND B) both true

  • IF (A OR B) at least one true

  • IF (NOT A) opposite


math + random

var num = randomNumber(1,6)
var remainder = num % 2

+ add - subtract * multiply / divide % remainder


functions

function greet() {
  console.log("hi")
}
greet()
  • define before calling

  • always use () when calling

  • wrap body in { }


debugging
console.log("check:", varName)
common errors:

  • missing ;

  • wrong variable name / case

  • unbalanced { } ( )

  • missing quotes


practice logic

if (speed > 60) → "Too fast!"
else → "Safe speed"

if (number % 3 == 0) → "Divisible by 3"

if (answer == "yes") → "You agreed!"
else → "You disagreed!"

flipCoin() → "Heads" or "Tails"

remember

  • if decides, else catches

  • && = both must be true

  • || = either one

  • ! = opposite

  • test your logic in english first


Not true means False


Not false means True


Not (Not A) means A


Not (A=B) means A !=B


A<=B means A<B OR A=B


A>=B means A>B OR A=B


NOT A AND NOT B means NOT(A OR B)


NOT (A AND B) means NOT A OR NOT B



XAND - eXclusive Not AND


NOR - Not OR


XOR - eXclusive Not OR