Combining itteration and arrays | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
For loops and arrays are a great way of simplifying your code. Consider the task of displaying which students did better on which test.
We previously had the code - Function compareTests(name As String, test1 As Number, test2 As Number) We can further simplify this by first storing all of the values into arrays first.
compareTests(names(1), Test1(1), Test2(1)) compareTests(names(2), Test1(2), Test2(2)) compareTests(names(3), Test1(3), Test2(3)) compareTests(names(4), Test1(4), Test2(4)) compareTests(names(5), Test1(5), Test2(5)) Finally we can then write a FOR loop to go through the array. FOR I=1 to 5 compareTests(names(I), Test1(I), Test2(I)) NEXT Now that we have the above for loop it is a easy task to increase the number of students. All that would happen is that the FOR I=1 to 5 would change to FOR I=1 to 200 if 200 students were considered. |