Lecture Outline - Information Management and · PDF fileWeek 5 – Lecture 1 and 2 ......
Transcript of Lecture Outline - Information Management and · PDF fileWeek 5 – Lecture 1 and 2 ......
www.monash.edu.au
IMS1906 Programming in VB.NETWeek 5 – Lecture 1 and 2
The repetition structure (Part I and II)
© Angela CarboneMonash University School of Information Management and Systems
www.monash.edu.au2
Lecture Outline
Repetition (looping)• The For Next Loop• The Do While Loop• The Repeat Until Loop
Application Development
www.monash.edu.au3
Control Structures
• 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
www.monash.edu.au4
Repetition Structures/Loops
• 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.au5
Repetition structures have …
• 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
– tested in the condition
www.monash.edu.au6
Repetition Structures
Pre-test Loops
(Do-while)
Post-testLoops
(Repeat-Until)
CountedLoops(For)
www.monash.edu.au7
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
number = 0DOWHILEnumber <10
display numbernumber = number + 1
ENDDO
www.monash.edu.au8
DOWHILE - Algorithm
DOWHILE a condition evaluates to TRUEstatement block
ENDDO
For example:number = 0DOWHILE number <10
display numbernumber = number + 1
ENDDO
www.monash.edu.au9
DO WHILE Loops
• 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
– otherwise endless loop (“infinite” loop)
www.monash.edu.au10
DOWHILE - Problem Example
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 displayedon the screen.
Celsius = (Fahrenheit – 32) x 5/9
www.monash.edu.au11
Defining Diagram
INPUT PROCESS OUTPUT
fahrenheit Read fahrenheit
Calculate celsius
Display celsius
celsius
Sometimes called an IPO chart (for Input, Processing, Output)
www.monash.edu.au12
DOWHILE - Solution Algorithm
Set temperature_count to zeroDOWHILE temperature_count < 15
Prompt for f_tempGet f_tempCompute c_temp = (f_temp - 32)* 5/9Display c_tempIncrement temperature_count
ENDDODisplay ‘All temperatures processed’ to the screenEND
Initialise the test variable
Test the condition:
Change the test variable
If condition is true, go here
Go
Bac
k If condition is false, go here
www.monash.edu.au13
DOWHILE - Desk Check
Input data: test value 1: f_temp = 32, c_temp = (f_temp - 32) * 5/9 = 0Expected outcome: c_temp = 0
test value 2: f_temp = 50c_temp = (f_temp - 32) * 5/9 = 10Expected outcome: c_temp = 10
www.monash.edu.au14
DO WHILE – Desk Checking
TRUE …DO WHILE
1Add
DisplayDisplay
0Compute
32Read
TRUEDO WHILE
0Initialize
DOWHILE condition
c_tempf_tempTemp_countStatement
www.monash.edu.au15
DO WHILE – Desk Checking …
END.
Display
FALSEDO WHILE
15Add
DisplayDisplay
10Compute
50Read
TRUEDO WHILE
14… Add
DOWHILE condition
c_tempf_tempTemp_countStatement
www.monash.edu.au16
Common Traps with Loops
• Forgetting to give initial values to loop counters
• Forgetting to change the condition variables inside the loop
www.monash.edu.au17
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)
number = 0REPEAT
display numbernumber = number + 1
UNTIL number = 10
www.monash.edu.au18
REPEAT UNTIL - Algorithm
REPEAT
statement blockUNTIL a condition evaluates TRUE
For example:number = 0REPEAT
Display numberIncrement number by 1
UNTIL number = 10
www.monash.edu.au19
• A Repeat Until in an algorithm becomes a DO .. LOOP UNTIL in VB.NET code:
Do…Statements…
Loop Until condition
REPEAT UNTIL – VB.NET Syntax
www.monash.edu.au20
Counted loops
• 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 10display i
END DO
www.monash.edu.au21
DO counter = m to n [ step x ]
statement blockEND DO
For example: Another example:DO i = 1 to 10 DO i = 1 to 11 step 2
display i display iEND DO END DO
DO … END DO - Algorithm
www.monash.edu.au22
Fahrenheit - Celsius conversion (Robertson, p.52)
Everyday, a weather station receives 15 temperaturesexpressed 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.
Counted Loops - Problem Example
www.monash.edu.au23
Counted Loops - Solution Algorithm
Fahrenheit_Celsius_conversionDO temperature_count = 1 to 15
Prompt operator for f_tempGet f_tempCompute c_temp = (f_temp - 32)* 5/9Display c_temp
END DODisplay ‘All temperatures processed’ to the screen
END
www.monash.edu.au24
FOR NEXT – VB.NET Syntax
For counter_var = m to nstatement block
Next counter_var
For counter_var = m to n step sstatement block
Next counter_var
www.monash.edu.au25
Hints for Programming with Loops
• Minimise the steps inside any loop – 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
• Enter the loop from only one location• Leave the loop only by satisfying the exit
condition– Structured programming requires this
www.monash.edu.au26
Repetition – Summary
• Pre-test loops: Do While …
• Post-test loops: Repeat … Until• Counted loops: For … Next
You have now learnt all the elements of the Structure Theorem.
www.monash.edu.au27
Questions/Reading
• 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
www.monash.edu.au28
TEST 1 (Next week) Overview
• Test will cover topics weeks 1-5
• Topics include:• Week 1
– Algorithm design – Structure Theorem– Steps in writing a program– Desk checking an algorithm
• Week 2– Objects and Classes– Properties and behaviours– VB Objects/Controls– VB.NET IDE – Built a simple splash screen
• Week 3– 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
• Week 4– Selection– If statements– Case statements
• Week 5Repetition (looping)– The For Next Loop– The Do While Loop– The Repeat Until Loop
www.monash.edu.au29
TEST 1 Overview
• Test will be 50 mins long• Counts for 10% of total mark• Question types
– 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!