SUNY Geneseo Department of Computer Science


“For” Loops, Day 3

Monday, March 10

CSci 120, Spring 2014
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture

Misc

Exam 1 ready to return

            function z = f( x , y )
                z = 3 * x - 2 * y;
            end
            
            >> f( 4, 8 )
            >> 10 * f( 3, 9)

Questions?

Nested loops?

        x = 0;
        for i = 1 : 10
            x = x + i;
            for j = 1 : 20
                y = i + j;
            end
            fprintf( '%d\n', i );
        end

Concrete example in Matlab: compute the sum of all the values in a matrix

A = [ 2 5 3; 9 4 10 ]
A =
    2     5     3
    9     4    10
sum = 0;
for r = 1 : 2
    for c = 1 : 3
        sum = sum + A(r,c)
    end
end
sum =
    2
sum =
    7
sum =
    10
sum =
    19
sum =
    23
sum =
    33
sum
sum =
    33

Edge and special cases for Riemann sums?

For Loops Lesson

Next

“While” loops

Read / watch

Handout


Next Lecture