Key Concepts

Review core concepts you need to learn to master this subject

break Keyword

numbers = [0, 254, 2, -1, 3] for num in numbers: if (num < 0): print("Negative number detected!") break print(num) # 0 # 254 # 2 # Negative number detected!

In a loop, the break keyword escapes the loop, regardless of the iteration number. Once break executes, the program will continue to execute after the loop.

In this example, the output would be:

  • 0
  • 254
  • 2
  • Negative number detected!
Lesson 1 of 2
  1. 1
    How do we use code to tell a computer this: “Create a variable and call a function 15 times”? We could write it out 15 times: create a variable call a function create a variable call a function …
  2. 2
    When we give instructions once and tell the computer how many times to repeat them, we are using a for loop, or a count-controlled loop. It executes a set of instructions for a specified numb…
  3. 3
    What if you approached tile-placing another way: you don’t know how many tiles to place, but you know when to stop. How could you communicate this type of command to a computer? Give it the instruc…
  4. 4
    There’s one more way to give looping instructions to a computer. We define a sequence of desired tile colors (a list), and tell the computer to repeat the instructions for each item in the sequen…
  5. 5
    Let’s combine all that we’ve learned so far: * variables, operators, and data types * functions and control flow * lists and loops Examples of for and while loops are provided below in JavaScript….
  6. 6
    Well done! Loops are commonly used in programming because they save time, reduce error, and are easy to read. Being comfortable with each type of loop will make you a better programmer. In review: …