Chapter One
Chapter One
N
for (int i = 1; i <= N; i++) {
}
sum = sum+i; 1 N
i 1
• Suppose we count the number of additions that are
done. There is 1 addition per iteration of the loop,
hence N additions in total.
Nested Loops: Formally
• Nested for loops translate into multiple
summations, one for each for loop.
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) { N M N
}
sum = sum+i+j; 2 2M 2MN
i 1 j 1 i 1
}