2018-06-17 - In IL: Print the Alphabet (do while, for)
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
Last time we looked at printing the alphabet using the while look. Today we are going to look at the do...while and for loops. First up we have the do...while loop which is vary similar to the while loop except that it checks the condition at the end of the loop instead of the beginning.
Note that because the condition is checked at the end of the loop the ch will be printed at least once regardless of its value. Now let's look at the compiled program.
This looks very similar to the while loop we had last time. The only difference is the lack of the branch to the condition check at the start of the loop which makes sense since the do while loop doesn't check the condition at the start of the loop.
Now let's look at the for loop version.
and the compiled code.
This looks identical to the while loop version which makes sense given that they are doing the exact same thing. The main difference between the for loop and the while loop is where things are put. In the while case only the condition is a part of the loop syntax, the initialization and stepping of the variable are done separately. In the for loop case all of these actions are a part of the syntax of the loop. We'll see more about the implications of this later on.
Next time we will look at some Visual Basic .NET loops.
Comments: