Imbedded Loops - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Imbedded Loops

Description:

There is an outer loop that keeps the program running until we ... Example: Create a multiplication table. 1 2 3 4 5 6 7 8 9 10. 2 4 6 8 10 12 14 16 18 20 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 8
Provided by: scie228
Category:

less

Transcript and Presenter's Notes

Title: Imbedded Loops


1
Imbedded Loops
Often, we have programs that run until we desire
to stop them. Class registration, data input
like student information, shopping list. There
is an outer loop that keeps the program running
until we indicate a stop. There can be several
loops inside the outer loop that process
information or do calculations.
Imbedded Loops Any loop (OUTER loop) which
contains a second loop (INNER loop).
2
Imbedded Loops
Consider a problem You have a class of students.
You want to calculate their average grade. The
number of recorded grades for each student is
different, because some opted to do an additional
project. Devise a program to that will calculate
the average, assign the letter grade, and account
for the difference in number of scores.
3
Imbedded Loops
  • Tasks of the problem
  • Loop for each student
  • Get student data
  • Loop for grades
  • Calculate grade total
  • Calculate grade average
  • Assign letter grade
  • Print results

4
Imbedded Loops
Start Program Header, Definition, Variables
Loop for Student Data
Student Data and number of grades
Loop for Grades
Get Grades
Calculate total
End Loop
Calculate Average
Assign Letter Grade
Print results
End Loop
5
Imbedded Loops
Start Program Header, Definition, Variables
Loop for Student Data
Student Data and number of grades
J - Loop
Loop for Grades
Note The Inner Loop must complete before the
Outer Loop
Get Grades
K - Loop
Calculate total
End Loop
Calculate Average
Assign Letter Grade
Print results
End Loop
6
Imbedded Loops
Example Create a multiplication table 1
2 3 4 5 6 7
8 9 10 2 4 6
8 10 12 14 16 18
20 . . . 10 20 30 40
50 60 70 80 90 100
200 FOR ROW 1 to 10 REM Outer Loop 210
FOR COL 1 to 10 REM Inner Loop 220
P ROW COL REM Calculate product 230
PRINT P 240 NEXT COL
REM close Inner Loop 250 PRINT
REM return cursor 260 NEXT ROW
REM close Outer Loop 999 END
7
Imbedded Loops
Imbedded loops are used extensively when we
control a program by user input. The outer loop
determines if the program should continue
running the inner loops execute program
functions.
  • Rules
  • The INNER loop needs to complete its execution
    before the outer loop.
  • Each loop must have a unique loop controller
Write a Comment
User Comments (0)
About PowerShow.com