The different types:
1. Endless loop
2. Loops with condition
3. For loop
This loop runs forever.
Syntax:
Endless Loop
This loop runs forever.
Syntax:
loop
[insert code to be repeated];
end loop;
Loops with Condition
There are three types of condition placement that a loop can use to continue running its code.
Condition in the:
1. beginning
2. middle
3. end
Condition in the beginning uses a test in the beginning to check if it should run a certain looping code. If the condition is met, it will run the code. If not, the code is never executed or if it has already run, will not loop through again.
Syntax:
while [condition] loop
[code to be repeated]
end loop;
Condition in the end uses a test to check if it should run a certain looping code more times. If the condition is met, the code will be repeated. The code in the loop will be ran at least once.
Syntax:
loop
[code to be repeated]
exit when [condition];
end loop;
The syntax:
for I in [low value] .. [high value] loop
[looping code]
end loop;
OR
for I in reverse [low value] .. [high value] loop
[looping code]
end loop;
The starting, end, and incrementing is specified using a Range.
The way in which the variable will increment (up or down) will depend if reverse is used.
The low value and high value MUST be come in the order specified. Low value first, then high value.
You can also loop on Arrays! Syntax:
for I in X'Range loop
[looping code]
[to use the current X, you would use X(I)]
end loop;
where X is the Array name.
Endless Loop:
The output:
Conditional Loops
Condition in beginning:
The output:
Condition in middle or end:
The output:
For Loops
Normal For loop:
The output:
Reverse For loop:
The output:
Condition in the:
1. beginning
2. middle
3. end
Condition in the beginning uses a test in the beginning to check if it should run a certain looping code. If the condition is met, it will run the code. If not, the code is never executed or if it has already run, will not loop through again.
Syntax:
while [condition] loop
[code to be repeated]
end loop;
Condition in the end uses a test to check if it should run a certain looping code more times. If the condition is met, the code will be repeated. The code in the loop will be ran at least once.
Syntax:
loop
[code to be repeated]
exit when [condition];
end loop;
Condition in the middle uses a test to check if it should continue to run a looping code or not. If the condition is met, the code will continue. If not, it will exit the loop. It is pretty much the same as the Until loop except the exit appears in the middle instead of the end.
Syntax:
loop
[code to be repeated]
exit when [condition];
[more code to be repeated]
end loop;
This is used for incrementing a specific variable to control the number of times a looping code is ran.Syntax:
loop
[code to be repeated]
exit when [condition];
[more code to be repeated]
end loop;
For Loop
The syntax:
for I in [low value] .. [high value] loop
[looping code]
end loop;
OR
for I in reverse [low value] .. [high value] loop
[looping code]
end loop;
The variable can be incremented up or down to any end value. Normally, the variable to be incremented is type Integer.
The starting, end, and incrementing is specified using a Range.
The way in which the variable will increment (up or down) will depend if reverse is used.
The low value and high value MUST be come in the order specified. Low value first, then high value.
You can also loop on Arrays! Syntax:
for I in X'Range loop
[looping code]
[to use the current X, you would use X(I)]
end loop;
where X is the Array name.
Examples
Endless Loop:
The output:
Conditional Loops
Condition in beginning:
The output:
Condition in middle or end:
The output:
For Loops
Normal For loop:
The output:
Reverse For loop:
The output:
No comments:
Post a Comment