Control Flow Flashcards

1
Q

What is flow control?

A

When writing programs, you want your data to take the correct path. You want it to turn left or right, up, down, reverse, or proceed straight ahead when it’s supposed to.

We call this flow control.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a conditional?

A

A conditional is a fork (or multiple forks) in the road.

They use the keywords ‘if’ and ‘else’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a clause in this context?

A

A block, statement, or expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are operands?

A

The expressions or values that an operator uses are its operands. In comparisons, the expressions to the left and right of the operator are the operands. For instance, the equality comparison x === y uses the === operator with two operands: x and y.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is this ‘===’ operator called?

A

The strict equality operator, also known as the identity operator, returns true when the operands have the same type and value, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is this ‘!==’ operator called?

A

The strict inequality operator returns false when the operands have the same type and value, true otherwise. Note that !== is the inverse of ===: when === returns true, !== returns false, and vice versa.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is this ‘==’ operator called?

A

The non-strict equality operator, also known as the loose equality operator, is similar to ===.

However, when the operands have different types, == attempts to coerce one of the operands to the other operand’s type before it compares them, and it may coerce both operands in some cases.

The result is true when the final values are the same, false otherwise. The coercion behavior can lead to unexpected results. For instance, when we compare the number 5 to the string ‘5’ using ==, we get true; with ===, we get false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is this ‘!=’ operator called?

A

The non-strict inequality operator, also known as the loose inequality operator, is similar to !==. However, when the operands have different types, != attempts to coerce one of the operands to the other operand’s type before it compares them, and it may coerce both operands in some cases. The result is false when the final values are the same, true otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is this ‘<’ operator called?

A

The less than operator returns true when the value of the left operand has a value that is less than the value of the right operand, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is happening here?

A

When comparing strings, the comparison is character-by-character. JavaScript moves from left-to-right in the strings looking for the first character that is different from its counterpart in the other string. Once it finds a character that differs, it compares that character with its counterpart, and makes a decision based on that. If both strings are equal up to the length of the shorter string as in the next to last example, then the shorter string is considered less than the longer string.

The final example shows that if you use < with two different types, some sort of coercion will take place. In this case, “42” gets coerced to a number, so a numeric comparison takes place. Don’t try to remember this.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is this ‘>’ operator called?

A

The greater than operator returns true when the value of the left operand has a value that is greater than the value of the right operand, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is this ‘<=’ operator called?

A

The less than or equal to operator returns true when the value of the left operand has a value that is less than or equal to the value of the right operand, false otherwise. Note that =< is not a valid comparison operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is this ‘>=’ operator called?

A

The greater than or equal to operator returns true when the value of the left operand has a value that is greater than or equal to the value of the right operand, false otherwise. Note that => is not a valid comparison operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is this ‘!’ operator called?

A

The not operator returns true when its operand is false and returns false when the operand is true. That is, it negates its operand. Note that, unlike most operators, ! takes a single operand; the operand appears to the right of the operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the ‘&&’ operator called?

A

The ‘and’ operator returns true when both operands are true and false when either operand is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the ‘||’ operator called?

A

The or operator returns true when either operand is true and false when both operands are false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is short circuit evaluation?

A

Consider these two expressions:

The first expression returns true when item is both red and portable. If either condition is false, then the overall result must be false. Thus, if the program determines that item is not red, it doesn’t have to check whether it is portable. JavaScript short-circuits the entire expression by terminating evaluation as soon as it determines that item isn’t red. It doesn’t need to call isPortable() since it already knows that the entire expression must be false.

18
Q

Why would it print ‘how can this be true’?

A

The first example logs “how can this be true?” while the second logs “it is not true.” This works since JavaScript coerces the value 5 to true, and the value 0 to false. To repeat, JavaScript can coerce any value to a boolean. Thus, you can use any expression in a conditional expression. We often say that the expression evaluates as or evaluates to true or false.

19
Q

What would be printed here?

A

The first example logs “how can this be true?” while the second logs “it is not true.” This works since JavaScript coerces the value 5 to true, and the value 0 to false. To repeat, JavaScript can coerce any value to a boolean. Thus, you can use any expression in a conditional expression. We often say that the expression evaluates as or evaluates to true or false.

20
Q

What are the JS versions of ‘false’?

A

-false

-The number 0. This includes all 3 variations of zero in JavaScript:
0: The ordinary zero value.
-0: A negative zero. That’s mathematical nonsense, but a real thing in JavaScript.
0n: The BigInt version of zero.

An empty string (‘’)

undefined

null

NaN

21
Q

What is truthy and falsy?

A

We often use the term falsy to refer to values that evaluate as false, while the values that evaluate as true are truthy. We use these terms when we need to distinguish between boolean true and false values. We can also discuss truthiness: whether something is a truthy or falsy value.

22
Q

What is the return value when using ‘&&’ and ‘||’ operators?

A

They use short circuit evaluation.

The return value will be the last evaluated operand.

23
Q

What is happening here?

A

In reality, !! isn’t a separate operator in JavaScript. Instead, it’s two consecutive ! operators. The expression !!a is equivalent to writing !(!a). The inner ! converts the value of a to false if it is truthy, or true if a is falsy. The outer ! then flips true to false or false to true. In the end, we end up with a boolean value instead of a truthiness value:

24
Q

What is this ‘??’ operator called?

A

The nullish coalescing operator evaluates to the right-hand operand if the left-hand operand is nullish (either null or undefined). Otherwise, it evaluates to the value of the left-hand operand.

25
Q

How is ‘||’ different from ‘??’ operator?

A

|| will return the right-hand operand if the left operand is a falsy value.

?? will return the right-hand operand if the left operand is nullish.

26
Q

When is ‘??’ operator useful?

A

?? is most useful when dealing with code that could result in undefined or null, and you want to handle the situation gracefully:

27
Q

How does JS evaluate expressions with several operators and sub-expressions?

A

JavaScript has a set of precedence rules it uses to evaluate expressions that use multiple operators and sub-expressions.

The following is a list of the comparison operations from the highest precedence (top) to lowest (bottom).

<=, <, >, >= - Comparison
===, !==, ==, != - Equality
&& - Logical AND
|| - Logical OR

28
Q

In what order does this expression get evaluated?

A

For the moment, let’s ignore the fact that both || and && are short-circuit operators.

The program first evaluates the y && z sub-expression since && has higher precedence than ||. It then takes the result of that evaluation and evaluates x || result.

29
Q

Can we override the precedence order in any way?

A

Yes! Using parenthesis.

We can use parentheses to override the precedence: sub-expressions in parentheses get evaluated before un-parenthesized expressions at the same depth in the main expression

30
Q

How does this expression get evaluated?

A

In this code, x || y gets evaluated first, and then result && z. That’s a different result from the un-parenthesized expression.

Parentheses help the computer and other programmers understand your intentions; you should strive to use parentheses in any expression that uses two or more different operators.

31
Q

What happens when there are multiple parenthesized subexpressions?

A

When multiple parenthesized subexpressions appear at the same depth, it evaluates them from left to right. Once it evaluates the parenthesized expressions, it evaluates the final expression value.

32
Q

What is the ‘ternary operator’?

A

The ternary operator is a quick and easy way to write a short, concise, and simple if/else conditional. It uses a combination of the ? and : symbols and takes 3 operands (hence, the name “ternary”):

33
Q

What is the biggest advantage of ternary operators vs if/else statements?

A

The chief advantage that the ternary operator has over an if/else statement is that the entire structure is an expression. What that means is that we can treat the ternary expression as a value: we can assign it to a variable, pass it as an argument, and so on. Since if/else is a statement, we can’t capture its result to a variable.

34
Q

When should I use a ternary expression?

A

Ternary expressions should usually be used to select between 2 values, not to choose between two actions. (An action would be something like logging a value to the console or setting a variable to a new value.) The ternary expression’s result should almost always be assigned to a variable, passed to a function as an argument, or returned by a function. If you’re not doing one of those things, an if/else statement is a better choice.

35
Q

Is this good practice?

A

No!

The following snippets use ternaries that choose between actions, and should be considered inappropriate uses.

36
Q

How does the ‘switch’ statement work?

A

A switch statement is similar to an if statement, but it has a different interface. It compares a single value against multiple values for strict equality (as with the === operator), whereas if can test multiple expressions with any condition.

37
Q

Why is the break statement important in a ‘switch’ statement?

A

The break statement in each case is crucial. Without a break, execution “falls through” to the next case clause.

38
Q

What is a fall-through in a switch statement?

A

When we execute multiple case clauses for a single value

39
Q

When are fall-throughs appropriate?

A

For instance, suppose you want to execute the same action for two or more cases:

40
Q

Is this a fall through?

A

Technically, this is fall-through, but, since each case executes a single clause, it’s safe to use and doesn’t suggest a possible error.