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

iOS

can someone explain this while loop to me

Can someone help explain to me why this would only print ola, ro without the b? I went through it in my head and i keep seeing that the b should be printed because i is only equal to 7 at that point in the loop

var str = "Hola, Rob"

var i = 1

while (i < 8)

{

print (Array(str.characters)[i])

i++

}

5 Answers

Hello Alexander:

A while loop will continue working until the condition given to it becomes false. So in this code you want the loop to go over some characters and stop at the last character.

Your code:

var str = "Hola, Rob"

var i = 1

while (i < 8) {
     print(Array(str.characters)[i])
     i++   

// If i is less than 8 print a character and repeat until it becomes false
// 7 is less than 8, but 8 is not less than 8, so loop stops at 7.
}

Hola, Rob has 9 characters, in which the space also counts as a character. Arrays have index numbers that start at 0.

  • H = 0,
  • O = 1
  • L = 2
  • A = 3
  • , = 4
  • (Space) = 5 ,
  • R = 6,
  • O = 7
  • B = 8

So the letter H is 0 by index, and the last letter b is 8 by index.

In your code you have a variable called i that has a value of 1, therefore if you start the loop at 1, then the first letter printed would be o.

When you instruct the loop i < 8 (While i is less than 8 print a character), but when it gets to 7 then it stops there, because 8 is not less than 8, therefore it becomes false, and then loop stops shy of one character.So in order to print those 2 characters that are not printing we have to do two things.

  • 1) Start the count at 0
  • 2) End the count at 9
var str = "Hola, Rob"

var i = 0

while (i < 9) {
    print(Array(str.characters)[i])
    i++
}

I hope this helps you understand a bit better.

I encourage you to use the votes up or down and best answer markers to help other students find good answers.

Good luck :)

Thank you Jhoan

"hola, rob" is 9 characters. the "b" is at index 8. Remember. Array's start couting at 0. "H" is 0. "o" is 1. the "," would be 5. and the "b" is 8. change the code to var i = 0; and change the while loop to while( i <9)

hey Derek,

Thanks for the reply. So the "" counts towards the character count in the array? If thats the case wouldnt the "H" be at index 1? since the quote would be at index 0?

No it does not. the two outer quotation marks are identifiers. they tell the compiler to use everything inside them. but the quotes themselves dont count and wont be displayed

Thats what i thought. So i guess i am still miscounting. If i = 1, that means we are starting the loop at the "o", right? So i still dont understand how it gets to 8 without it printing out the "b" at the end. I am obviously missing something, but i want to understand.

Did you edit the while loop? if you still have while ( i < 8) it wont actually reach 8. because 8 < 8 is not true. it will stop running at 7. you need to change your loop to while( i < 9) or while (i <=8)

hey Derek,

I should of been more clear, apologies. This isnt code i am using at all, it was just code that i saw which i didnt understand because i dont get how the "b" in rob is not being printed when from what i see i = 7 once the loop got to the "b". Basically i am just tying to break this down to understand it completely.