WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

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      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.