LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm:

Slides:



Advertisements
Similar presentations
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
CS107: Introduction to Computer Science Lecture 2 Jan 29th.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms  Readings: [SG] Ch. 2 & 3  Chapter Outline: 1.Chapter Goals 2.What are Algorithms 1.Real Life.
Chapter 3 IFTHENELSE Control Structure © 2008 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved. Marilyn Bohl/Maria Rynn Tools for Structured.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Algorithms (more examples…)  Supplementary Notes: 1.For your reference… (esp. those new to programming)
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science 1620 Loops.
REPETITION (loops or iteration) Schneider: Sec. 6.1 & 6.3.
Fall 2004COMP 3351 Recursively Enumerable and Recursive Languages.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Recursively Enumerable and Recursive Languages
Designing Algorithms February 2nd. Administrativia Lab assignments will be due every Monday Lab access –Searles 128: daily until 4pm unless class in progress.
Designing Algorithms Csci 107 Lecture 4.
LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 © Leong Hon Wai, Algorithms Problem Solving  Readings: [SG] Ch. 2  Chapter Outline: 1.Chapter.
5.05 Apply Looping Structures
CS0007: Introduction to Computer Programming Introduction to Arrays.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
CSCI-100 Introduction to Computing Algorithms Part I.
LeongHW, SoC-USP, NUS (UTT2201: Introduction) Page 1 © Leong Hon Wai, UIT2201: Lecture 1  Lecture Outline: 1.The Pervasive Computer 2.Examples.
LeongHW, SoC, NUS (UIT2201: Database) Page 1 © Leong Hon Wai, Animation of SQL Queries To illustrate three SQL queries: –Q1: simple select (one.
LeongHW, SoC, NUS (UIT2201: AI) Page 1 © Leong Hon Wai, Integrating Different Ideas Together  Reading Materials:  Ch 3.6 of [SG]  Contents:
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Logic Structure - focus on looping Please use speaker notes for additional information!
By the end of this session you should be able to...
Ways to Check for Divisibility Dividing By 1 All numbers are divisible by 1.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
© 2006 Pearson Education 1 More Operators  To round out our knowledge of Java operators, let's examine a few more  In particular, we will examine the.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
ITEC113 Algorithms and Programming Techniques
Repetition Control Structures Simple Program Design Third Edition A Step-by-Step Approach 5.
CSCI-100 Introduction to Computing
Lab 2 Parallel processing using NIOS II processors
Ways to Check for Divisibility Dividing By 1 All numbers are divisible by 1.
Control Structures RepetitionorIterationorLooping Part I.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
© Leong Hon Wai, LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution,
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
COMP Loop Statements Yi Hong May 21, 2015.
Recursively Enumerable and Recursive Languages
Ways to Check for Divisibility Dividing by 2 All even numbers are divisible by 2 Even numbers are numbers that end with either 2, 4, 6, 8, or 0.
Tutorial 2 Control Flow: Loops NUS SCHOOL OF COMPUTING CS1010E PROGRAMMING METHODOLOGY 1 CS1010E TUTORIAL SLIDES PREPARED BY WU CHAO.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Computer Program Flow Control structures determine the order of instruction execution: 1. sequential, where instructions are executed in order 2. conditional,
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm & Flow Charts Decision Making and Looping
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
CS2100 Computer Organisation
Programming Logic and Design Fourth Edition, Comprehensive
Java's for Statement.
Introduction To Flowcharting
Error Handling Summary of the next few pages: Error Handling Cursors.
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
The Linux Command Line Chapter 29
Arrays & Functions Lesson xx
Repetition and Loop Statements
Systems Architecture I (CS ) Lecture 1: Random Access Machines
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
Faculty of Computer Science & Information System
Loop Construct.
The structure of programming
Systems Architecture I (CS ) Lecture 1: Random Access Machines
Wrapup which is the document write that says the end.
Presentation transcript:

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 1 Animation of Algorithm Goal: To understand an algorithm by animating its execution, step-by-step. Algorithm: Sum 1-to-5 (find sum from 1 to 5) (Note: Similar to Sum 1-to-100, but shorter!!) Observe carefully: osequential operations/statements oconditional statements, oiterative statements,

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 2 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Let’s animate the execution of this simple algorithm.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 3 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 0. CPU sum ??? Our abstract model of the computer Initial state of the algorithm

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 4 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k ??? Step 1. Sum  0; CPU sum 0 Start of execution, at Step 1. Assignment statement; The value of 0 is stored in the storage box called sum. Assignment statement; The value of 0 is stored in the storage box called sum.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 5 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 2. k  1; CPU sum 0 Assignment statement; The value of 1 is stored in the storage box called k. Assignment statement; The value of 1 is stored in the storage box called k. Executing Step 2.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 6 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 1 Step 3. sum + k = CPU sum 1 Assignment statement; The new value of sum is stored; the old value is gone. Assignment statement; The new value of sum is stored; the old value is gone. Executing Step 3. start of “body-of-loop”

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 7 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 4. k + 1 = CPU sum 1 Assignment statement; The new value of k is stored; the old value is gone. Assignment statement; The new value of k is stored; the old value is gone. Executing Step 4. inside “body-of-loop”

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 8 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 5. (k > 5)? (2 > 5)? = NO CPU sum 1 Condition check: evaluate (k > 5)? NO  execute Step 6 next. Condition check: evaluate (k > 5)? NO  execute Step 6 next. Executing Step 5. loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 9 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 6. goto repeat (Step 3) CPU sum 1 goto “repeat” means to get algorithm to continue at the step labelled “repeat”. goto “repeat” means to get algorithm to continue at the step labelled “repeat”. Executing Step 6.

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 10 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 2 Step 3. sum + k = CPU sum 3 Add 2 to sum; The new value of sum is stored; the old value is gone. Add 2 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 2 nd round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 11 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 3 Step 4. k + 1 = CPU sum 3 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 2 nd round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 12 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 3 Step 5. (k > 5)? (3 > 5)? = NO CPU sum 3 Condition check: evaluate (k > 5)? NO  execute Step 6 next. Condition check: evaluate (k > 5)? NO  execute Step 6 next. Executing Step 5. 2 nd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 13 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 3 Step 6. goto repeat (Step 3) CPU sum 3 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 2 nd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 14 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 3 Step 3. sum + k = CPU sum 6 Add 3 to sum; The new value of sum is stored; the old value is gone. Add 3 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 15 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 4 Step 4. k + 1 = CPU sum 6 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 3 rd round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 16 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 4 Step 5. (k > 5)? (4 > 5)? = NO CPU sum 6 Condition check: evaluate (k > 5)? NO  execute Step 6 next. Condition check: evaluate (k > 5)? NO  execute Step 6 next. Executing Step 5. 3 rd loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 17 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 4 Step 6. goto repeat (Step 3) CPU sum 6 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 3 rd round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 18 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 4 Step 3. sum + k = CPU sum 10 Add 4 to sum; The new value of sum is stored; the old value is gone. Add 4 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 19 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 5 Step 4. k + 1 = CPU sum 10 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 4 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 20 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 5 Step 5. (k > 5)? (5 > 5)? = NO CPU sum 10 Condition check: evaluate (k > 5)? NO  execute Step 6 next. Condition check: evaluate (k > 5)? NO  execute Step 6 next. Executing Step 5. 4 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 21 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 5 Step 6. goto repeat (Step 3) CPU sum 10 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step 6. 4 th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 22 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 5 Step 3. sum + k = CPU sum 15 Add 5 to sum; The new value of sum is stored; the old value is gone. Add 5 to sum; The new value of sum is stored; the old value is gone. Executing Step 3. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 23 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 6 Step 4. k + 1 = CPU sum 15 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step 4. 5 th round of loop-body 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 24 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 6 Step 5. (k > 5)? (6 > 5)? = YES CPU sum 15 Condition check: evaluate (k > 5)? YES  execute Step 7 next. Condition check: evaluate (k > 5)? YES  execute Step 7 next. Executing Step 5. 5 th loop-test 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 25 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish finish: print out the value of sum k 6 Step 7. goto finish (Step 8) CPU sum 15 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 26 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish finish: print out the value of sum k 6 Step 8. Print to output CPU sum 15 Print statement; print to output the value of sum Print statement; print to output the value of sum Executing Step 8. print output and END 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; Output of Algorithm: 15

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 27 Summary  Summary of Steps: o1, 2, (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,6), (3,4,5,7), 8  Note the sequential execution, except for oConditional statements oGoto statements oiterative statements  Questions: oWhere is the “loop-body”? oHow many iteration of the loop-body? oHow many times is the loop-test done?

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 28 Explore further (DIY)  We did Sum 1-to-5 (instead of Sum 1-to-100)  DIY: Simulate the execution for the original algorithm for Sum 1-to-100?  (Use the following “ending”-slides to help you.)

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 29 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 5. (k > 100)? (100 > 100)? = NO CPU sum 5050 Condition check: evaluate (k > 100)? NO  execute Step 6 next. Condition check: evaluate (k > 100)? NO  execute Step 6 next. Executing Step th loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 30 Simulating an Algorithm ALGORITHM Sum-1-to-5; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 5)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum k 100 Step 6. goto repeat (Step 3) CPU sum 4950 Goto Step 3 and Execute the loop-body again. Goto Step 3 and Execute the loop-body again. Executing Step th round 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15;

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 31 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 100 Step 3. sum + k = CPU sum 5050 Add 100 to sum; The new value of sum is stored; the old value is gone. Add 100 to sum; The new value of sum is stored; the old value is gone. Executing Step th round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 32 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 4. k + 1 = CPU sum 5050 Increment k; The new value of k is stored; the old value is gone. Increment k; The new value of k is stored; the old value is gone. Executing Step th round of loop-body

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 33 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no  goto repeat 7. yes  goto finish Finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 5. (k > 100)? (101 > 100)? = YES CPU sum 5050 Condition check: evaluate (k > 100)? YES  execute Step 7 next. Condition check: evaluate (k > 100)? YES  execute Step 7 next. Executing Step th loop-test

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 34 Simulating an Algorithm ALGORITHM Sum-1-to-100; 1. sum  0 2. k  1 repeat: add k to sum 4. add 1 to k 5. Is (k > 100)? 6. no  goto repeat 7. yes  goto finish finish: print out the value of sum 0+1=1; 1+2=3; 3+3=6; 6+4=10; 10+5=15; k 101 Step 7. goto finish (Step 8) CPU sum 5050 Goto finish (Step 8) (exit the iterative loop!) Goto finish (Step 8) (exit the iterative loop!) Executing Step 7. & exit the iterative loop

LeongHW, SoC, NUS (UIT2201: Algorithms) Page 35