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      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.