Visual Basic loop structures allow you to run one or more
lines of code repetitively. You can repeat the statements in a loop
structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
The following illustration shows a loop structure that runs a set of statements until a condition becomes true.
The While...End While construction runs a set of statements as long as the condition specified in the While statement is True. The following illustration shows a loop structure that runs a set of statements until a condition becomes true.
Running a set of statements until a condition becomes true
Do Loops
The Do...Loop
construction allows you to test a condition at either the beginning or
the end of a loop structure. You can also specify whether to repeat the
loop while the condition remains True or until it becomes True.
For Loops
For Loops
The For...Next construction performs the loop a set number of times. It uses a loop control variable, also called a counter,
to keep track of the repetitions. You specify the starting and ending
values for this counter, and you can optionally specify the amount by
which it increases from one repetition to the next.
For Each Loops
For Each Loops
The For Each...Next
construction runs a set of statements once for each element in a
collection. You specify the loop control variable, but you do not have
to determine starting or ending values for it.
//==============================================================
Loops allow us to repeat code.
Syntax for while loop:
while (condition)
{
code inside the while loop
}
the code inside the loop will repeat until the condition is false.
Notes:
//==============================================================
Loops allow us to repeat code.
Syntax for while loop:
while (condition)
{
code inside the while loop
}
the code inside the loop will repeat until the condition is false.
Notes:
- Pay attention very closely to the condition to make sure that the condition will turn to false at some point to avoid endless loop.
- Condition is just a boolean expression or a boolean variable
- If you know the number of times to iterate, then you need to make sure of 3 things.
- Have a counter variable and initialize it with some value. Do that before the loop
- Set the condition where the counter should stop
- Update the counter towards the condition
No comments:
Post a Comment