2018-01-20 - In IL: Loop Instructions
In IL
- Part 1 - Introduction
- Part 2 - Variables and Types
- Part 3 - Variables in Visual Basic .NET
- Part 4 - Instructions and the Stack
- Part 5 - Volume of a Cylinder (Operations)
- Part 6 - Branching Instructions
- Part 7 - Largest of Two Numbers (if-else)
- Part 8 - Largest of Three Numbers (If-ElseIf-Else)
- Part 9 - Switch instruction
- Part 10 - Grade Analyser (switch)
- Part 11 - Prize Calculator (switch-2)
- Part 12 - VB Grade Analyser (Select)
- Part 13 - Loop Instructions
- Part 14 - Print the Alphabet (while)
- Part 15 - Print the Alphabet (do while, for)
- Part 16 - Print the Alphabet (Do Until)
- Part 17 - Print the Alphabet (break, continue)
- Part 18 - Array Instructions
- Part 19 - Summing Arrays
- Part 20 - Other Instructions
- Part 21 - Assemblies
- Part 22 - Class Definitions
- Part 23 - C# Classes and Structs
- Part 24 - VB Classes, Modules and Structures
- Part 25 - Field Definitions
- Part 26 - Field Declarations
This post will be really simple because there are no IL loop instructions. As we shall see loops in IL are implemented using branching instructions just like the conditional statements we saw previously. The main difference is that conditionals tend to jump forward while loops tend to jump backwards so that instructions can be re-ran.
So the question is why isn't there any loop instructions? Well let's think about what these loop instructions would need to do. There is a large variety of programing languages out there and those languages have very different ideas about how they want their loops to work. In order for the IL loop instructions to be useful they would need to meet the majority of these scenarios which would require a large number of very complex looping instructions. So instead IL provides simple branching instructions that can be combined to create complex looping statements. Keeping IL simple and providing more power to languages that want to target IL.
Next time we will look at the simple while loop.
Comments: