Lecture Outline IMS1906 Programming in VB.NET Week 5 – Lecture 1 and 2 Repetition (looping) • The For Next Loop • The Do While Loop • The Repeat Until Loop The repetition structure (Part I and II) © Angela Carbone Monash University School of Information Management and Systems Application Development www.monash.edu.au 2 www.monash.edu.au Control Structures Repetition Structures/Loops • The Structure Theorem (from Week 1):any computer program can be built from just three control structures: – Sequence – Selection – Repetition – This Study Guide • Used to determine (control) the order in which the steps in an algorithm are to be executed • Some problems require a sequence of tasks to be processed repeatedly, e.g.: – apply same processing to a set of different data items – processing until and target point is reached • Repetition structures (aka loops, iteration structures) cater for this www.monash.edu.au www.monash.edu.au 3 4 Repetition structures have … Repetition Structures • A block of statements that are to be repeated • A condition that will determine if the loop processing should stop • the condition is evaluated once in each iteration • A variable that will change each time the loop is processed Counted Loops (For) Pre-test Loops (Do-while) – tested in the condition Post-test Loops (Repeat -Until) www.monash.edu.au www.monash.edu.au 5 6 The Pre-Test Loop • • • • Condition is tested PRIOR to the execution of the statement block If the condition is TRUE: – Perform the statement block (perform one iteration) – Go back to the condition (to check again) If the condition is FALSE: – “exit” from the loop (do not perform any more iterations) The loop may potentially not be executed: – if condition fails on first test DOWHILE - Algorithm number = 0 DOWHILE number <10 display number number = number + 1 ENDDO DOWHILE a condition evaluates to TRUE statement block ENDDO For example: number = 0 DOWHILE number <10 display number number = number + 1 ENDDO www.monash.edu.au www.monash.edu.au 7 8 DO WHILE Loops DOWHILE - Problem Example • If the condition evaluates to FALSE initially, no iterations are performed • To ensure at least one iteration, variables tested in condition need to be initialised prior to the loop • At least one variable tested in the condition must change within the statement block Fahrenheit - Celsius conversion (Robertson, pp. 51-54) Everyday, a weather station receives 15 temperatures expressed in Fahrenheit. A program is to be written which will accept each Fahrenheit temperature, convert it to Celsius and displaythe converted temperature to the screen. After 15 temperatures have been processed, the words ‘All temperatures processed’ are to be displayed on the screen. Celsius = (Fahrenheit – 32) x 5/9 – otherwise endless loop (“infinite” loop) www.monash.edu.au www.monash.edu.au 9 10 Defining Diagram INPUT fahrenheit DOWHILE - Solution Algorithm PROCESS Read fahrenheit OUTPUT celsius Calculate celsius Go Back Display celsius Sometimes called an IPO chart (for Input, Processing, Output) Initialise the test variable Set temperature_count to zero Test the condition: DOWHILE temperature_count < 15 If condition is true, go here Prompt for f_temp If condition is false, go here Get f_temp Compute c_temp = (f_temp - 32)* 5/9 Display c_temp Increment temperature_count Change the test variable ENDDO Display ‘All temperatures processed’ to the screen END www.monash.edu.au www.monash.edu.au 11 12 DOWHILE - Desk Check DO WHILE – Desk Checking Input data: test value 1: f_temp = 32, Statement Temp_count Initialize 0 f_temp DO WHILE c_temp = (f_temp - 32) * 5/9 = 0 32 Compute test value 2: f_temp = 50 Add Expected outcome: c_temp = 10 Temp_count 14 f_temp c_temp DO WHILE www.monash.edu.au 13 14 DOWHILE condition • Forgetting to give initial values to loop counters • Forgetting to change the condition variables inside the loop 10 Display Add Common Traps with Loops 50 Compute TRUE … www.monash.edu.au TRUE Read Display 1 DO WHILE DO WHILE – Desk Checking … … Add 0 Display c_temp = (f_temp - 32) * 5/9 = 10 DOWHILE condition TRUE Read Expected outcome: c_temp = 0 Statement c_temp Display 15 DO WHILE FALSE Display END. www.monash.edu.au www.monash.edu.au 15 16 The Post-Test Loop • • • • The condition is tested at the END of each iteration of the loop – At least one iteration will be performed If the condition evaluates to FALSE, another iteration will be executed If the condition is TRUE, exit from the loop Expressed by: – REPEAT … UNTIL loops (for algorithms) – Do … Loop Until (for Visual Basic .NET) REPEAT UNTIL - Algorithm number = 0 REPEAT display number number = number + 1 UNTIL number = 10 REPEAT statement block UNTIL a condition evaluates TRUE For example: number = 0 REPEAT Display number Increment number by 1 UNTIL number = 10 www.monash.edu.au www.monash.edu.au 17 18 REPEAT UNTIL – VB.NET Syntax Counted loops • • A Repeat Until in an algorithm becomes a DO .. LOOP UNTIL in VB.NET code: • • Do … Statements … • • Loop Until condition www.monash.edu.au www.monash.edu.au 19 20 DO … END DO - Algorithm DO counter = m to n Counted Loops - Problem Example [ step x ] Fahrenheit - Celsius conversion (Robertson, p.52) Everyday, a weather station receives 15 temperatures expressed in Fahrenheit. A program is to be written which will accept each Fahrenheit temperature, convert it to Celsius and displaythe converted temperature to the screen. After 15 temperatures have been processed, the words ‘All temperatures processed’ are to be displayed on the screen. statement block END DO For example: DO i = 1 to 10 display i END DO Execute the statement block a pre-determined number of times – Number of iterations known in advance A control variable keeps count of the number of repetitions – No need to change this explicitly in code May use i, j & k as control variables (historical) – meaningful names better Do .. End Do (algorithms) FOR…NEXT loops (VB.NET) DO i = 1 to 10 display i END DO Another example: DO i = 1 to 11 step 2 display i END DO www.monash.edu.au www.monash.edu.au 21 22 Counted Loops - Solution Algorithm FOR NEXT – VB.NET Syntax Fahrenheit_Celsius_conversion DO temperature_count = 1 to 15 Prompt operator for f_temp Get f_temp Compute c_temp = (f_temp - 32)* 5/9 Display c_temp END DO Display ‘All temperatures processed’ to the screen END For counter_var = m to n statement block Next counter_var For counter_var = m to n step s statement block Next counter_var www.monash.edu.au www.monash.edu.au 23 24 Hints for Programming with Loops Repetition – Summary • Minimise the steps inside any loop • Pre-test loops: Do While … • Post-test loops: • Counted loops: Repeat … Until For … Next – Keep It Simple! • Make each loop perform one function • Document (Comment) the loop by: – Stating the conditions for which the loop executes, or – Explicitly stating the termination conditions You have now learnt all the elements of the Structure Theorem. • Enter the loop from only one location • Leave the loop only by satisfying the exit condition – Structured programming requires this www.monash.edu.au www.monash.edu.au 25 26 Questions/Reading • • TEST 1 (Next week) Overview Questions – Hammer in the key concepts – Pull out those questions – List the three types of loops and when they should be used. – Do you know the syntax of each loop? Reading – Unit Guide, Study Guide 5 – Zak, Chpt 5 – Robertson Chpt 5 • • Week 1 – – – – • • Objects and Classes Properties and behaviours VB Objects/Controls VB.NET IDE Built a simple splash screen Week 3 – – – – – – – – Algorithm design Structure Theorem Steps in writing a program Desk checking an algorithm Week 2 – – – – – • Week 4 – – – • TOE Charts Designing User Interface In-built functions: Val(), Format() Classes Convert and Random Variables/Data Types Literals and Constants Option Explicit and Implicit Input/Output Selection If statements Case statements Week 5 Repetition (looping) – The For Next Loop – The Do While Loop – The Repeat Until Loop www.monash.edu.au www.monash.edu.au 27 28 TEST 1 Overview • Test will be 50 mins long • Counts for 10% of total mark • Question types – – – – • Test will cover topics weeks 1-5 Topics include: Short answer written responses Writing pseudo code Develop TOE chart + User Interface VB.NET coding questions • Wishing you all the success you deserve! • Reminder TEST 1 starts at 10am next week! www.monash.edu.au 29