0% found this document useful (0 votes)
14 views20 pages

Class 9 FlowchartForLoops

The document discusses the concept of for loops in programming, emphasizing their use for executing a specified number of iterations with a loop counter. It explains how for loops can be utilized to perform operations such as updating variables or processing elements in a matrix, and introduces the idea of nested loops. Additionally, it covers the importance of pseudocode and top-down design in algorithm development.

Uploaded by

kenfornaija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views20 pages

Class 9 FlowchartForLoops

The document discusses the concept of for loops in programming, emphasizing their use for executing a specified number of iterations with a loop counter. It explains how for loops can be utilized to perform operations such as updating variables or processing elements in a matrix, and introduces the idea of nested loops. Additionally, it covers the importance of pseudocode and top-down design in algorithm development.

Uploaded by

kenfornaija
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CLASS 9:

FLOWCHARTS – FOR LOOPS


ENGR 102 – Introduction to Engineering
2 for Loop

Webb ENGR 102


for Loop
3

 We’ve seen that the number of while loop iterations is


not known ahead of time
 May depend on inputs, for example
 Sometimes we want a loop to execute an exact,
specified number of times
 A for loop
 Utilize a loop counter
 Increment (or decrement) the counter on each iteration
 Loop until the counter reaches a certain value

 Can be thought of as a while loop with the addition of a


loop counter
 But, a very distinct entity when implemented in code

Webb ENGR 102


for Loop
4

 Initialize the loop counter


 i, j, k are common, but name
does not matter
 Set the range for i
 Not necessary to define
variable istop
 Execute loop instructions, A
 Increment loop counter, i
 Repeat until loop counter
reaches its stopping value
 Continue on to B
Webb ENGR 102
for Loop
5

 for loops are counted loops


 Number of loop iterations is
known and is constant
 Here loop executes 10 times
 Stopping value not
necessarily hard-coded
 Coulddepend on an input or
vector size, etc.

Webb ENGR 102


for Loop
6

 Loop counter may start at


value other than 1
 Increment size may be a
value other than 1
 Loop counter may count
backwards
Iteration cntr Process
1 6 A
2 4 A
3 2 A
4 0 A
5 -2 A
6 -4 B

Webb ENGR 102


for Loop – Example 1
7

 Here, the loop counter, i, is used


to update a variable, x, on each
iteration
Iteration i x
1 0 0
2 1 1
3 2 4
4 3 9
5 4 16
 When loop terminates, and flow
proceeds to the next process
step, x = 16
 A scalar
 No record of previous values of x
Webb ENGR 102
for Loop – Example 2
8

 Now, modify the loop process to store


values of x as a vector
 Use loop counter to index the vector

i X[i] x
0 0 [0]
1 1 [0, 1]
2 4 [0, 1, 4]
3 9 [0, 1, 4, 9]
4 16 [0, 1, 4, 9, 16]

 When loop terminates,


x = [0, 1, 4, 9, 16]
 A vector
 x grows with each iteration
Webb ENGR 102
for Loop – Example 3
9

 The loop counter does not


need to be used within the
loop
 Used as a counter only
 Here, a random number is
generated and displayed
each of the 10 times through
the loop
 Counter,i, has nothing to do
with the values of the
random numbers displayed
Webb ENGR 102
for Loop – Example 4
10

 Have a vector of values, x


 Find the mean of those
values
 Sum all values in x
 A for loop
# of iterations equal to the
length of x
 Loop counter indexes x

 Divide
the sum by the
number of elements in x
 After exiting the loop
Webb ENGR 102
11 Nested Loops

Webb ENGR 102


Nested Loops
12

 A loop repeats some process some number of times


 The repeated process can, itself, be a loop
 A nested loop

 Can have nested for loops or while loops


 Can nest for loops within while loops and vice versa
 One application of a nested for loop is to step
through every element in a matrix
 Loop counter variables used as matrix indices
 Outer loop steps through rows (or columns)

 Inner loop steps through columns (or rows)

Webb ENGR 102


Nested for Loop – Example
13

 Recall how we index the elements within a matrix:


 𝐴𝐴𝑖𝑖𝑖𝑖 is the element on the 𝑖𝑖 𝑡𝑡𝑡 row and 𝑗𝑗𝑡𝑡𝑡 column of the matrix 𝐴𝐴
 Using Python syntax: A[i,j]
 Consider a 3 × 2 matrix
−2 1
𝐵𝐵 = 0 8
7 −3
 To access every element in 𝐵𝐵:
 start on the first row and increment through all columns
 Increment to the second row and increment through all columns
 Continue through all rows
 Two nested for loops

Webb ENGR 102


Nested for Loop – Example
14

−2 1
𝐵𝐵 = 0 8
7 −3
 Generate a matrix 𝐶𝐶
whose entries are the
squares of all of the
elements in 𝐵𝐵
 Nested for loop
 Outer loop steps through
rows
 Counter is row index
 Inner loop steps through
columns
 Counter is column index
Webb ENGR 102
15 Pseudocode & Top-Down Design

Webb ENGR 102


Pseudocode
16

 Flowcharts provide a useful tool for designing


algorithms
 Allow for describing algorithmic structure
 Ultimately used for generation of code

 Details neglected in favor of concise structural and


functional description
 Pseudocode provides a similar tool
 One step closer to actual code
 Textual description of an algorithm

 Natural language mixed with language-specific syntax

Webb ENGR 102


Pseudocode – Example
17

 Consider an algorithm for


determining the maximum of a
vector of values
 Pseudocode might look like:
N = length of x
max_x = x[0]
for i = 1 through N-1
if x[i] is greater than current
max_x, then set max_x = x[i]

 We'll learn the Python-specific


for-loop syntax in the following
section of notes

Webb ENGR 102


Top-Down Design
18

 Flowcharts and pseudocode are useful tools for top-


down design
 A good approach to any complex engineering design (and
writing, as well)
 First, define the overall system or algorithm at the top level
(perhaps as a flowchart)
 Then, fill in the details of individual functional blocks

 Top-level flowchart identifies individual functional


blocks and shows how each fits into the algorithm
 Each functional block may comprise its own flow chart or
even multiple levels of flow charts
 Hierarchical design

Webb ENGR 102


Top-Down Design - Example
19

 Let’s say you have deflection data from FEM


analysis of a truss design
 Data stored in text files
 Deflection vs. location along truss
 Parametric study
 Three different component thicknesses
 Two different materials
 Six data sets

 Read in the data, calculate the max deflection and


plot the deflection vs. position

Webb ENGR 102


Top-Down Design - Example
20

Level 1: Level 2: Level 3:

Webb ENGR 102

You might also like