
CSIS 110 - Lecture 17
Quiz Results:
Mean: 20.3
Median: 21.25
Max: 24.5
Distribution: <17.5: 3 17.5-19.5: 1 20-22: 6 22.5-24.5: 6 25: 0
Last Time:
Arrays
- what is an array?
- How do we declare an array variable?
- How do we allocate the array elements?
- How do we access an individual element?
- What is the initial value of an array element?
For Loop
We've seen two types of loops: foreach and while. There is a third variety of loop: the for
loop. This loop is also commonly used with arrays.
for ( ForInit; ForExpression; ForUpdate )
Loop Body
[Draw flowchart]
Here is an example:
for ( int i = 5; i < 30; i = i + 7 ) {
System.out.print( i + " " );
}
And here is an example that uses an array:
int[] quizScores = new int[ numStudents ];
int sum = 0;
for ( int index = 0; index < quizScores.length; index++ )
{
sum = sum + quizScores[ index ];
}
Why would we use this loop instead of a foreach or while? Use it when we
- Want to execute loop a fixed number of times. (like foreach)