Filtering Arrays Tutorial

From Progzoo
Jump to navigation Jump to search

You may use either style of for loop to run over the array. You can control which items are dealt with using an if clause:

Print Big Items

Print each item that is larger than 3.

For the array [2, 7, 5, 3] you should print:

7
5


[Font] [Default] [Show] [Resize] [History] [Profile]

Print Small Items

Print each item that is smaller than 4 - otherwise print "big".

For the array [2, 7, 5, 3] you should print:

2
big
big
3


[Font] [Default] [Show] [Resize] [History] [Profile]

Mark Small Index Items

Print each item and print * if the index of the item is less than 3.

For the array [2, 7, 5, 3] you should print:

2*
7*
5*
3


[Font] [Default] [Show] [Resize] [History] [Profile]

Mark Items Larger than the Index

Print each index and the item. If the item is larger than the index print * on the end of the line.

For the array [2, 7, 5, 3] you should print:

0 2*
1 7*
2 5*
3 3


[Font] [Default] [Show] [Resize] [History] [Profile]