Introduction to Lists and Iteration
Discussion Question: How would you build a slide-show of images/text?
Instructor Demo: if-else solution to this problem
-
Add a button and upload three images
-
set Button.BackgroundImage to first image in designer
-
In blocks editor, use if-else-else to sequence through images on each button click. Go back to first image after last.
-
Note how file name of loaded image is used and must be set exactly.
Discuss if-else solution
-
Would it be easy to add another item?
-
Could the images shown be user generated?
List Solution
Store images in a list variable. Use an index variable to walk through the list
-
List-- a single variable (name) representing multiple memory cells
-
Index -- a number representing the current position in the list. 1 is the first position.
-
Important List functions: select list item, add items to list
Instructor demo: sketch or code blocks for letting user walk through list
-
start index at 1
-
on screen.initialize:
-
on nextButton.Click
-
reaching the end of list
Discussion: Compare If-Else and List solution
- What if number of items changes, which is easier to change?
- What if user enters images, will if-else solution work?