From the course: Code Clinic: Ruby

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Loops with backtracking

Loops with backtracking

From the course: Code Clinic: Ruby

Start my 1-month free trial

Loops with backtracking

- In the last movie, we took our first stab at this problem by using simple loops. We did okay for our first attempt. In the process we realized that we really needed to gain more control over the looping process. Ruby offers us a nice variation on loops. The conditional loop, in this case, while. So you can see in the example, I've got n equals zero, and then while n is less than eight, keep looping. So inside the loop, I'm going to do some code and some business, and then I'm going to increment n. N is going to now going to increase in value by one, that's what that plus equals means, it means take the existing value and increment it by this amount. So the first time through, n is equal to zero, then it's equal to one, two, three, four, five until finally n is equal to eight. And at that point the loop does not execute because now n is no longer less than eight so it just exits, it drops right down below the end and continues with any code that may be below that. The results of this…

Contents