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

While loop is not very clear

When explaining the while loop, a lot of new items and functions are introduced (like .count, incrementing while ++, etc.) which makes the point not quite comprehensive. I feel kinda lost now since I cannot track the progress and don't know if I should continue the course.

2 Answers

int i = 0;
int j = 10;

while ( i < j )
{
    println("Hello there");
    i++;
}

println("While is done, goodbye");

It like this:

-On line 1 we declare integer variable i with value = 0 -Line 2 we declare integer variable j with value = 9 -Line 4: you start the while loop: -the while loop first checks if the test is TRUE meaning that i=0 and it is smaller that j=10 so this test is true and it runs the command in the while body

  • it prints out the string and increments the i variable by 1 ( i++ is the same as i= i+1;)
  • now it reaches the end of the body and goes back again to the while line and tests again if the statement is true
  • when i = 10 the while test comes back FALSE because 10 is not smaller than 10 but the same so the loop stops and the programs continues on the last line which prints that the loops is done.

Hope this helped....

Got it. Thank you very much for a very clear explanation!!

Sure you are welcome.