-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreak Keyword
52 lines (40 loc) · 1.37 KB
/
Break Keyword
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// We can use the break keyword to exit out of the loop before the loop fully completes:
let respect = 556
for pageNum in 1...1000 {
if pageNum == respect {
print("Respect means: to admire someone for their abilities.")
break
}
print("On page \(pageNum) and still no 'respect'!")
}
//Output
On page 1 and still no 'respect'!
On page 2 and still no 'respect'!
...
On page 555 and still no 'respect'!
Respect means: to admire someone for their abilities.
//Example
// To complete the program, in the for-in loop:
// Add an if statement that checks if counter is equal to guessedNum.
// Inside the if body, print out the "It's [guessedNum]!!" and replace [guessedNum] with the actual value of guessedNum.
// After the print() statement, add a break keyword.
// After you pass this step, run the program a few times to get a sense of when break is exiting the loop.
var guessedNum = Int.random(in: 1...15)
for counter in 1...15 {
// Add your code below 🤔
if counter == guessedNum {
print ("It's \(guessedNum)!!")
break
}
print("Is it \(counter)?")
}
//Output (Break point will keep changing every time
Is it 1?
Is it 2?
Is it 3?
Is it 4?
It's 5!!
// Note - .lowercased() is a method that can be called on strings to convert the string to a lowercase string. For instance:
let yell = "AHHH"
let whisper = yell.lowercased()
print(whisper) // Prints: ahhh