L11 Conditionals

Post date: May 30, 2020 1:11:13 AM

Overview

The class starts by using booleans to compare the current value of a sprite property with a target value, using that comparison to determine when a sprite has reached a point on the screen, grown to a given size, or otherwise reached a value using the counter pattern. After using booleans directly to investigate the values or sprite properties, the class adds conditional if statements to write code that responds to those boolean comparisons.

Vocab

Boolean Expression - in programming, an expression that evaluates to True or False.

If-Statement - The common programming structure that implements "conditional statements".

​​New Code

Comparison operators. True and False Statements. Why these symbols: ==, !=, <=, and >=?We use == because the single equal sign = is the assignment operator. We need something different to indicate we want to compare two values instead of assigning one to the other.Common mistake: writing something like if (age = 18) instead of if (age == 18). We'll make sure we get this down later.We use !=, <=, and >= because they only require ASCII symbols. Historically the mathematical symbols ≠, ≤ and ≥ were hard or impossible to produce on some systems. The ! is universally read as "not".Examples:Compares two values - numbers, strings, or other booleans - and returns true if they are equal, otherwise false."Hello" == "hello" returns false -- because the strings are capitalized differently."3" == 3 returns true -- because == tries to be forgiving. If it can "coerce" a string into a number it will do so to compare. 1

(2+1) == 3 returns true -- because the arithmetic expression evaluates to 3.

x == 7 returns true -- when the variable x has the value 7.

In section 5 you are changing only the parameters of the two sprites so that the Boolean expressions evaluate are all true. You may need to change one back to true when you change one. In section 7, you will need to create a console.log condition that starts off as false but changes to true when the race car crosses the finish line. The consol.log is a tool for you to have your program keep track of a number or parameter. In section 11, you will use an "if statement" to create text for the winner. If the raceCar.X < finishLine X then create text at size 50 100 X 100Y and state Winner