For Loop Review

For Loop Review#

These questionss are designed to asssess your current understanding of for loops!

Questions 1

True or False: For loops are typically used as definitive loops.


Question 2

Let’s write a for loop header! Say I’m writing a for loop that should loop six times. Which test(s) ensures my code will loop six times?

  1.  for (int num = 1; num <= 5; num++) {
    
  2.  for (int num = 1; num < 6; num = num + 1) {
    
  3.  for (int num = 1; num <= 6; num++) {
    
  4.  for (int num = 1; num < 7; num += 1) {
    

Question 3

How many hearts will the following for loop print?

for (int hearts = 1; hearts <= 25; hearts += 1) {
    System.out.println("<3");
}

Question 4

What is the output of this for loop?

Write the output in quotation marks. For example: “Hello!”

Hint

Watch out for trailing whitespace… 👀

What is trailing whitespace? Trailing whitespace is any whitespace after the last non-whitespace character in a line until the newline character, if any.

for (int counter = 0; counter <= 5; counter++) {
    System.out.print(counter + " ");
}

Question 5

[Challenge] Ho many stars ill be printed by the folloing for loop?

for (int stars = -5; stars <= 0; stars = stars + 1) {
    System.out.print("***");
}

Hint

Try following the control flow of the for loop and writing out how the value of stars changes as we progress.


Question 6

[Challenge] What is the output of this for loop?

for (int loop = 10; loop >= 0; loop -= 2) {
    System.out.print(loop + " ");
}