David Stotts Computer Science Department UNC Chapel Hill Introduction to Programming (in JavaScript)
Mar 22, 2016
David StottsComputer Science Department
UNC Chapel Hill
Introduction to Programming
(in JavaScript)
0. data (types, simple information)
1. data storage (variables, assignment)2. data retrieval (expressions, evaluation)3. repetition (loops)4. decision making (conditionals)5. procedure abstraction (functions)6. data abstraction (arrays)
7. objects: all-the-above, wrapped up
The Big Six(6) Data Abstraction
We have been studying Arrays
We have seen “for loops” as access patterns
Contains any valid statements…◦ assignment◦ conditionals
for (i=0; i<length; i++) { sum = sum + grade[ i ] ; if (grade[i]>90) { aCount = aCount + 1; } }
Loop Body
Another complete loop?What if loop body contains…
Loops in Loops
We call them “nested ” loops
for (i=0; i<length; i++) { . . . for (k=i; k<length; k++) { . . . }
. . . }
Nested For Loops
outer loop, enclosing
inner loop, enclosed in outer
Anyone from DC?
This is the fabulous Capital Beltway...
A.K.A...
Capital Beltway
The “Inner Loop”
and the “Outer Loop”
Inner and Outer Loops
for (i=0; i<length; i++) { . . . for (k=i; k<length; k++) { . . . } . . .
}
inner loop
outer loop
Nested For Loops
for (i=0; i<length; i++) { . . . for (k=i; k<length; k++) { . . . } . . .
}
inner loop
outer loop
Nested For Loops
1 outer iteration
many inner iterations
Formatting is CRITICAL for comprehension
for (i=0; i<length; i++) {sum = sum + grade[ i ] ;if (grade[i]>90) {aCount = aCount + 1; } else { bfCount = bfCount + 1; }total += 1;}
A Note on Indentation
Indent, block structure, be consistentfor (i=0; i<length; i++) { sum = sum + grade[ i ] ; if (grade[i]>90) { aCount = aCount + 1; } else { bfCount++; } total += 1;}
A Note on Indentation