Title: Computer Programming Looping Structures
1Computer ProgrammingLooping Structures
- Mr. Blochowski
- http//www.sfstoledo.org/classes/vbasic/csvb.htm
2Looping Structure
When a program repeats a group of statements a
number of times, the repetition is accomplished
using a loop.
The code required to create a loop is sometimes
called an iteration structure.
3Types of Looping Structures
2 Types of Looping Structures A. Definite
loop repeats a set number of times. B.
Indefinite loop repeats an unknown number of
times.
4Indefinite Loops
- Do. . .While is a looping structure that
executes a set of statements as long as a
condition is TRUE. - 2. Do. . .Until is a looping structure that
repeats a block of code until a condition becomes
TRUE.
5SYNTAX
Do While (boolean expression) statements Loop
Example intValue 1 Do While (intValue lt
10) intValue intValue 1 Loop
6SYNTAX
Do statements Loop Until (Boolean Expression)
Example intValue 1 Do intValue intValue
1 Loop Until (intValue gt 10)
7Infinite Loops
In an infinite loop, the condition that is
supposed to stop the loop from repeating never
becomes True. A logic error could lead to an
infinite loop.
Example intValue 1 Do While (intValue lt
10) intValue intValue 1 Loop
8Nested Loops
Nested Loop statements contain a looping
structure inside another looping
structure. Example intValue 1 Do While
(intValue lt 100) intSum 1 Do intSum
intSum 1 Loop Until (intSum gt 5) intValue
intValue intSum Loop
Note Nested statements should be indented for
good programming style.
9Definite Looping Structure
The ForNext statement is a looping structure
that executes a set of statements a fixed number
of times.
Syntax For counter start To end statements Next
counter
Counter, start, and end are integer variables,
values, or expressions. Counter is initialized
to start(counter start)only once when the loop
first executes, and compared to end before each
loop iteration. Counter is automatically
incremented by 1 after each iteration of the loop
body (statements).
10For..Next Statement
Example Dim intCounter As Integer For intCounter
1 To 10 Print intCounter Next intCounter
11Nested For Loops
For intOuter 1 To 10 For intInner1 1 To
2 Code goes here Next intInner1 For
intInner2 1 To 4 Code goes here Next
intInner2 Next intOuter
The indentation of the code helps you to identify
which Next statement is paired with each For
statement.
12Input box
An input box is a Visual Basic predefined dialog
box that has a prompt, a text box, and OK and
Cancel buttons. It is used to get information
from the user.
SYNTAX InputBox(prompt, title)
Prompt is a String variable or a string enclosed
in quotation marks. Title is an optional String
variable or a string enclosed in quotation marks.
When the OK button is selected, the data in the
text box is returned by the InputBox function.
13(No Transcript)