21 LoopStructures

21 LoopStructures - Control Structures: Loops CEE 3804...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Kamal B. Rojiani 1 Control Structures: Loops CEE 3804 Computer Applications for CEE
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Kamal B. Rojiani 2 Loops z There are basically two types of loops z Number of repetitions is know ahead of time z Number of repetitions is determined as runtime
Background image of page 2
Kamal B. Rojiani 3 While /Wend Loops While ( logical expression ) Wend x = 1 While x <= 20 x = x + 1 Wend Loop body These statements are executed as long as the condition is true Must change x inside the loop Initialize
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Kamal B. Rojiani 4 While/Wend Loop z Loop control variable must change within the body of the While/Wend loop y = 0 x = 1 While y >= 0 z = x * 2 sum = sum + z Wend The value of y was not changed in the loop so there is no exit from the loop.
Background image of page 4
Kamal B. Rojiani 5 While/Wend Example Dim score as Integer Dim maxScore as Single Dim minScore as Single Dim sum as Single Dim numScores as Integer ‘ initialize maxScore = 0 minScore = 0 numScores = 0 sum = 0
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Kamal B. Rojiani 6 Example of While/Wend Loop While score >= 0 ‘ determine minimum and maximum If score > maxScore Then maxScore = score End If If score < minScore Then minScore = score End If ‘ compute sum and increment counter sum = sum + score numScores = numScores + 1 ‘ read next score .. .. Wend
Background image of page 6
Kamal B. Rojiani 7 For/Next Loops z General Form For counter = initVal To finalVal Step stepSize Next counter counter - variable that controls number of repetitions initVal - initial value or starting value finalVal - final value stepSize - increment Loop Body
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Kamal B. Rojiani 8 For/Next Loops For i = 1 To 10 Console.Write(“ {0}”,i) Next i 1 2 3 4 5 6 7 8 9 10
Background image of page 8
Kamal B. Rojiani
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2008 for the course CEE 3804 taught by Professor Aatrani during the Fall '07 term at Virginia Tech.

Page1 / 26

21 LoopStructures - Control Structures: Loops CEE 3804...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online