Learning Goal 2 (Lessons 5-8)

32 important questions on Learning Goal 2 (Lessons 5-8)

What operators can be used to add or subtract any value to the current value of a variable?

We can use the += or -= operator to add or subtract any value we want to the current value of a variable.

How can you express stopping once you reach the gas station using a while loop?

Keep going while you have NOT reached the gas station
while(!(x == y)){...}

What are the shorthand versions of count = count + 3 and count = count - 3?

  • count += 3
  • count -= 3
  • Higher grades + faster learning
  • Never study anything twice
  • 100% sure, 100% understanding
Discover Study Smart

What must the code inside a while loop do to prevent an infinite loop?

The code inside the loop must change something about the state of the program - usually the value of a variable - so that eventually the boolean expression becomes false.

What is the purpose of the ++ operator in programming?

As programmers we are always looking for more concise ways to write code. It is so common to add 1 to a variable such as in count = count + 1 that there is actually a shorthand for it.

What do the statements count = count + 1, count++, and count += 1 have in common?

Notice that these 3 statements: count = count + 1, count++, and count += 1 all do exactly the same thing!

What do if statements create within our program logic?

If statements create a "branch" within our program logic, only running some code if a condition is true.

Why might our code not be able to tell if it is doing the right thing?

Sometimes our code will be doing the right thing but we won’t be able to tell because of the way we are printing values.

What does the shorthand count++ do?

You can write count++ to add 1 to count. count++ does the exact same thing as count = count + 1!

How does the -= operator work compared to +=?

The -= operator works almost identically to +=, but instead it subtracts the value provided from the variable.

What is the problem if the condition of the while loop starts out false?

If the condition of the while loop starts out false then the loop will never run.

How can you write count = count - 1 using shorthand?

As you may have guessed, just as we can write count = count + 1 as count++ we can also write count - 1 as count--.

How can we ensure the loop condition evaluates to true the first time?

The solution is to initialize the values used in our boolean expression so that we are certain it will evaluate to true the first time the loop checks it.

What shorthand operators are introduced for adding or subtracting values other than 1?

Introducing += and -=!

What is the scenario described for rolling a pair of dice?

Scenario: If you roll a pair of dice, rolling a 12 (two sixes) is rare. How rare? If you were to roll a pair of dice 1,000 times, on average, how many times would it come up as 12?

What is a logic error?

A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly.

What should you do to the value of num inside the loop?

You need to make sure you set the value of num right away inside the loop.

What steps are suggested to figure out how often a 12 comes up when rolling dice 1,000 times?

  • Make a loop that simulates rolling a pair of dice 1,000 times.
  • Inside the loop, add an if statement: if die1 + die2 == 12, then add 1 to a counter.
  • After the loop, display the result.

How can you translate "until loops" into while loops in JavaScript?

Use the NOT (!) operator to translate stopping conditions into while loop conditions.

What is the purpose of loops in coding?

Loops are a powerful coding tool that can give us the power to automate lots of complex coding behaviors. One common use for loops are in simulations of real-life situations.

How many times does the loop run in the given code segment?

The loop ran 15 times.

What can we add inside the for loop to check every value within the list?

We can add an if statement inside the for loop to individually check every value within the list.

What is the technique of using a variable as a flag generally referred to as?

The technique is generally referred to as using a boolean "flag."

How would programmers typically read a loop written as for (var i = 0; i < 10; i++)?

Programmers would typically read a loop for (var i = 0; i < 10; i++) out like this: "for variable i starting at 0, while i is less than 10, i plus plus (or increment i by 1)". Notice that in reading a for loop we still use the word "while".

What is the output for the first iteration of the outer loop when x = 0?

0 0

Why is the single character i often used as the variable in a for loop?

Using the single character i as the variable in a for loop has become a convention in programming for a variety of reasons. One reason is that for loops are often used when processing lists - you can think of i as shorthand for index. But there is no reason why you have to use i if you don’t want to. It’s just a variable.

How can we use variables as indexes in a list with a for loop?

As you know, we can use variables as indexes in an list. We can take advantage of this fact to create a for loop which visits every index in a list. for(var i = 0; i < list.length; i++)

What does the for loop do in the context of updating values in a list?

This for loop basically means "for every possible index in myList..." and we use it as a basic building block for processing lists. Common list-processing techniques like searching for a value, updating all values, or calculating simple stats on an list will all be completed using a for loop written with the syntax above.

What is the initial value of the flag variable before processing the list?

We will create a variable before the loop and assign it false to start (flag is down).

What is one limitation of simulations mentioned in the text?

Simulations are only as good as their design, however, and are limited by the complexity of their code and the factors that are programmed into them.

What is the explanation for the nested loop behavior in the code segment?

For every value of x in the outer for loop, the inner for loop runs to completion and then resets for the next value of x.

How does the inner loop behave in a nested loop?

The inner loop will run to completion FOR EACH PASS of the outer loop (and will reset before the next pass).

The question on the page originate from the summary of the following study material:

  • A unique study and practice tool
  • Never study anything twice again
  • Get the grades you hope for
  • 100% sure, 100% understanding
Remember faster, study better. Scientifically proven.
Trustpilot Logo