Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alex Parsons
Courses Plus Student 214 PointsUsing While Loops
Here is an example of the while loop that i am attempting to write.
let y = 0 y++ while x <= 10 { println("Repeat") }
It worked when i took out the y++ postfix unary but it crashed my computer. So i wanted to print it out only 10 times. For some reason it is not working
I get two errors the first one for y++ it says "cannot invoke '++' with an argument of type 'int'. The second error i get states "use of unresolved identifier 'x'."
Thanks for the help in advance.
1 Answer
Stone Preston
42,016 Pointsyou need to increment inside the loop. also you need to use y, not x. you have not defined a variable x which is why you get that error:
var y = 0
while y < 10 {
println("Repeat")
y++
}
in the loop above, you set y to 0. then you say "while y is less than 10, print out "repeat" and then increment y by 1. so the loop checks to make sure that y is less than 10, then runs the code in the loop body. then it checks again, then runs it again etc etc until y is no longer less than 10. at that point the loop stops
Alex Parsons
Courses Plus Student 214 PointsAlex Parsons
Courses Plus Student 214 Pointshmm for some reason i am still getting the error for the y++. It still says "cannot invoke '++' with an argument of type 'int'. "
Stone Preston
42,016 PointsStone Preston
42,016 Pointswoops, you need to declare y as a var, not a constant. if its a constant you cant increment it