

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 8
click for more info
Not enough gems
Cost: 6 gems
1: Loops in Go
incomplete
2: Omitting Conditions from a for Loop in Go
incomplete
3: There Is No While Loop in Go
incomplete
4: Fizzbuzz
incomplete
5: Continue & Break
incomplete
6: Connections
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Whenever we want to change the control flow of a loop we can use the continue and break keywords.
The continue keyword stops the current iteration of a loop and continues to the next iteration. continue is a powerful way to use the guard clause pattern within loops.
for i := 0; i < 10; i++ {
if i % 2 == 0 {
continue
}
fmt.Println(i)
}
// 1
// 3
// 5
// 7
// 9
The break keyword stops the current iteration of a loop and exits the loop.
for i := 0; i < 10; i++ {
if i == 5 {
break
}
fmt.Println(i)
}
// 0
// 1
// 2
// 3
// 4
As an Easter egg, we decided to reward our users with a free text message if they send a prime number of text messages this year.
Complete the printPrimes function. It should print all of the prime numbers up to and including max. It should skip any numbers that are not prime.
Here's the pseudocode:
printPrimes(max):
for n in range(2, max+1):
if n is 2:
n is prime, print it and skip to next n
if n is even:
n is not prime, skip to next n
isPrime = true
for i in range (3, sqrt(n) + 1, 2):
if n can be evenly divided by i:
isPrime = false
break from inner loop
if isPrime:
print n
This is a primality test.
n. A factor higher than the square root of n must multiply with a factor lower than the square root of n, meaning we only need to check up to the square root of n for potential factors.
i * i <= nThis lesson is graded based on the output of your program, so don't leave any debugging print statements in your code.
For the first test case, prime number up to 10:
Primes up to 10:
2
3
5
7
===============================================================
We only want you to print the numbers themselves, not the headings and delimiter, they are already handled for you in the test code.
break keyword to exit the loop early.