UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day 6 - How to use a while loop to repeat work: Infinite loop problem

Forums > 100 Days of SwiftUI

Here is the example code I entered into my 100 Days of SwiftUI playground -

var id = Int.random(in: 1...20)

var roll = 0

while roll != 20 { // roll = id roll = Int.random(in: 1...20) print("I rolled a (roll)") } print("Critical hit!")

The while loop works as written, but if I comment out roll = Int.random(in: 1...20) And uncomment roll = id it creates an infinite loop and Xcode crashes.

I'm not sure why the var id doesn't work the same for either statement.

2      

HIiii! You need to put a \ before the (roll) so that it reads "I rolled a \(roll)" . String interpolation.... I don't know who thinks up words like interpolation, makes me think of bees for some reason, which has nothing to do with coding. Anyway, I'm not sure if that is your specific problem because of the way you have posted your code. While you are placing code in this forum you'll need to use the code button above </> so that when you insert code it looks correct to us coders, otherwise looking at your code above you have to use ; to seperate by lines...If you meant to have it all on one line that is. Try copy and pasting this into your playground and commenting out your own set of mnemonics, I'm pretty sure you'll figure out the issue/differences if it's not the \ causing it:

import SwiftUI

var id = Int.random(in: 1...20)

var roll = 0

while roll != 20 {roll = id
    roll = Int.random(in: 1...20)
    print("I rolled a \(roll)") }
    print("Critical hit!")

2      

I'll restate the question using code button: Here is the example code I entered into my 100 Days of SwiftUI playground -

var id = Int.random(in: 1...20)

var roll = 0

while roll != 20 {
//    roll = id
    roll = Int.random(in: 1...20)
    print("I rolled a \(roll)")
}
print("Critical hit!")

The while loop works as written, but if I comment out

roll = Int.random(in: 1...20) 

And uncomment

roll = id 

it creates an infinite loop and Xcode crashes.

I'm not sure why setting the value of the variable roll to var id doesn't work the same for either statement.

2      

Because roll never changes inside your loop and so it never has a chance to equal 20 and end the loop.

Whereas, if you use this code:

roll = Int.random(in: 1...20)

roll gets set to a new Int every time through the loop and therefore has the possibility of eventually hitting 20 and ending the loop.

2      

id gets set to a new Int every time through the loop and therefore has the possibility of eventually hitting 20 and ending the loop.

Do you mean roll get a new Int not id

2      

Yep, @NigelGee, that's right. My bad. I'll edit that.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.