English subtitles

← Loops on Lists - Intro to Computer Science

Get Embed Code
5 Languages

Showing Revision 6 created 05/25/2016 by Udacity Robot.

  1. [Evans] The final major concept we're going to introduce in this unit
  2. is how to loop through a list.
  3. Since loops are collections of things, it's very useful to be able to go through a list
  4. and do something with every element of the list.
  5. With the things we know so far, we already know enough to do this
  6. if we put them together in the right way.
  7. You should remember the while loop from the previous unit.
  8. And as a reminder, this is the way the while loop works.
  9. We have a while followed by a test expression,
  10. which is something that evaluates to true or false.
  11. And when it evaluates to a true value, that means to execute the block,
  12. which can be a block of any number of statements we want.
  13. And then at the end of the block we continue, go back to the while loop,
  14. try the test again, and we keep executing the block as long as the test evaluates to true.
  15. So for this quiz we're going to see if you can figure out how to use while
  16. to go through the elements of a list.
  17. We're going to provide most of the code for you.
  18. Our goal is to define a procedure called printallelements
  19. that takes as input a list.
  20. We'll use the name p for the list.
  21. Our goal is for that procedure to print out every element in the list.
  22. I've written most of the code for this for you,
  23. but I've left out the test expression for the while.
  24. So the code that's written introduces a variable named i,
  25. initializes this value to 0, and we're going to use i to index through the elements of the list.
  26. For each element we print out that value, and then we increase i + 1.
  27. So can you figure out the test expression we need for the while
  28. to make printallelements work,
  29. and what printallelements should do is print out every element in the input list p.