While Loops! The value of the variable i AFTER the highlighted line has been executed is shown here.
i = 3 while  i > 0 puts i.to_s i = i – 1 end the variable i is assigned an initial value of 3 i = 3
i = 3 while  i > 0 puts i.to_s i = i – 1 end i currently has a value of 3 making i > 0  true i = 3
i = 3 while  i > 0 puts i.to_s i = i – 1 end the while condition is true,  step into the code block i = 3
i = 3 while  i > 0 puts i.to_s i = i – 1 end subtract 1 from the i variable i = 2
i = 3 while  i > 0 puts i.to_s i = i – 1 end the end of the code block has been reached return to the while statement i = 2
i = 3 while  i > 0 puts i.to_s i = i – 1 end i currently has a value of 2 making i > 0  true i = 2
i = 3 while  i > 0 puts i.to_s i = i – 1 end i = 2
i = 3 while  i > 0 puts i.to_s i = i – 1 end i = 1
i = 3 while  i > 0 puts i.to_s i = i – 1 end return to the while statement the end of the code block has been reached i = 1
i = 3 while  i > 0 puts i.to_s i = i – 1 end i currently has a value of 1 making i > 0  true i = 1
i = 3 while  i > 0 puts i.to_s i = i – 1 end i = 1
i = 3 while  i > 0 puts i.to_s i = i – 1 end i = 0
i = 3 while  i > 0 puts i.to_s i = i – 1 end the end of the code block has been reached return to the while statement i = 0
i = 3 while  i > 0 puts i.to_s i = i – 1 end i is now equal to 0 making i > 0  false i = 0
i = 3 while  i > 0 puts i.to_s i = i – 1 end i is now equal to 0 making i > 0  false because the while condition is false,...
Upcoming SlideShare
Loading in …5
×

While Loops

733 views

Published on

Loops can be difficult for beginning programmers to grasp. This slide show walks through the execution of a while loop step by step.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
733
On SlideShare
0
From Embeds
0
Number of Embeds
108
Actions
Shares
0
Downloads
16
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

While Loops

  1. 1. While Loops! The value of the variable i AFTER the highlighted line has been executed is shown here.
  2. 2. i = 3 while i > 0 puts i.to_s i = i – 1 end the variable i is assigned an initial value of 3 i = 3
  3. 3. i = 3 while i > 0 puts i.to_s i = i – 1 end i currently has a value of 3 making i > 0 true i = 3
  4. 4. i = 3 while i > 0 puts i.to_s i = i – 1 end the while condition is true, step into the code block i = 3
  5. 5. i = 3 while i > 0 puts i.to_s i = i – 1 end subtract 1 from the i variable i = 2
  6. 6. i = 3 while i > 0 puts i.to_s i = i – 1 end the end of the code block has been reached return to the while statement i = 2
  7. 7. i = 3 while i > 0 puts i.to_s i = i – 1 end i currently has a value of 2 making i > 0 true i = 2
  8. 8. i = 3 while i > 0 puts i.to_s i = i – 1 end i = 2
  9. 9. i = 3 while i > 0 puts i.to_s i = i – 1 end i = 1
  10. 10. i = 3 while i > 0 puts i.to_s i = i – 1 end return to the while statement the end of the code block has been reached i = 1
  11. 11. i = 3 while i > 0 puts i.to_s i = i – 1 end i currently has a value of 1 making i > 0 true i = 1
  12. 12. i = 3 while i > 0 puts i.to_s i = i – 1 end i = 1
  13. 13. i = 3 while i > 0 puts i.to_s i = i – 1 end i = 0
  14. 14. i = 3 while i > 0 puts i.to_s i = i – 1 end the end of the code block has been reached return to the while statement i = 0
  15. 15. i = 3 while i > 0 puts i.to_s i = i – 1 end i is now equal to 0 making i > 0 false i = 0
  16. 16. i = 3 while i > 0 puts i.to_s i = i – 1 end i is now equal to 0 making i > 0 false because the while condition is false, the code block is skipped and in this case, the program ends i = 0

×