Overview Repetition structure Sentinelcontrolled repetition while loop do

  • Slides: 14
Download presentation

Overview • Repetition structure • Sentinel-controlled repetition • while loop • do. . .

Overview • Repetition structure • Sentinel-controlled repetition • while loop • do. . . while loop / repeat. . . until • Counter-controlled repetition (this week) • for loop (this week) • nested loop (this week) 2

for loop • เปนลปชนด repetition Counter-controlled counter initial condition(counter yes ) instruction increase/decrea se

for loop • เปนลปชนด repetition Counter-controlled counter initial condition(counter yes ) instruction increase/decrea se counter s. . . no. . . 5

for loop [cont. ] start J=1 for j = 1 to 5 write j

for loop [cont. ] start J=1 for j = 1 to 5 write j end for no J <= 5 for ( j=1 ; j<=5 ; j++ ) { yes write j write J stop } end for J = J+1 8

nested loop • เชนเดยวกบ nested if ในลปสามารถมลปซอนกนได for outer = 1 to 2 for

nested loop • เชนเดยวกบ nested if ในลปสามารถมลปซอนกนได for outer = 1 to 2 for inner = 1 to 3 write outer , inner end for (inner) end for (outer) outer 1 1 1 2 2 2 inner 1 2 3 12

13 start outer = 1 outer loop outer<=2 no yes outer = outer+1 inner

13 start outer = 1 outer loop outer<=2 no yes outer = outer+1 inner = 1 inner loop noouter<=2 inner = inner+1 yes write outer, inner stop

Referen ce • Maureen Sprankle - Problem Solving and Programming Concepts (9 th Edition)

Referen ce • Maureen Sprankle - Problem Solving and Programming Concepts (9 th Edition) , Prentice Hall. 14