:
: Thanks much for the in depth reply. It really sheds some light on the
: whole situation.
:
: If, in both cases, n is decremented in the context of the boolean
: check, then wouldn't it be possible just to do something like the
: following:
:
: If the check was "While (--n >= 0), then in VB it should be:
:
: Do While (n - 1 >= 0)
: n -= 1
: do some code
: Loop
:
: If the check was "While (n-- >= 0), then in VB it should be:
:
: Do While (n >= 0)
: n -= 1
: do some code
: Loop
:
: I believe that would handle both cases, but please correct me if I'm
: wrong.
At first glance this would in seem to be the case. And as a practical
matter, such an approach may suffice for your needs. It depends on
whether or not the final value of 'n' is relevant to your code once the
loop is complete.
If n is a throw away variable, then you could use the code above without
concern. However, if you are looking at the value of n at some point
after the code executes, you'll get different results with the C# code
than you will with the VB code. Consider:
--------------------------------------------------
Imports System
Public Class [Class]
Public Shared Sub Main()
Dim n As Integer = 2
Do While n - 1 >= 0
n -= 1
Console.WriteLine("n = " & n)
Loop
Console.WriteLine("n when the loop exits: " & n)
Console.WriteLine()
n = 2
Do While n >= 0
n -= 1
Console.WriteLine("n = " & n)
Loop
Console.WriteLine("n when the loop exits: " & n)
End Sub
End Class
--------------------------------------------------
In this case, the output is:
n = 1
n = 0
n when the loop exits: 0
n = 1
n = 0
n = -1
n when the loop exits: -1
The same number of loops occurs as in the C# code, but the final value
of 'n' is different. This is because the value of n is not modified as
part of the boolean check as it is when you use n-- or --n in the C#
code.
Ralf
: -Jason
:
: : >
: > : > :
: > : : > :
: > : >I know that in C#, the -- and ++ operators can act as prefix and
: > : >postfix operators. With that in mind, what differences would
: > : >appear between the following two sections of code when
: > : >translating them to VB?
: > : >
: > : > While (--n >= 0) {
: > : > do some code
: > : > }
: > : >
: > : > While (n-- >= 0) {
: > : > do some code
: > : > }
: > : >
: > : > I can't take these straight into VB, as VB doesn't allow
: > : > assignments within statements, so I have to decrement n
: > : > somewhere in the loop. Should be easy, I'm just confused
: > : > as to where to decrement n.
: > :
: > : You'll need to place the code to change n based on the following:
: > : Your first example used the prefix decrement operator which
: > : behaves as follows...
: > :
: > : The value of n is decremented, *then* the Boolean expression is
: > : evaluated. For example, if n were == 0, it would be decremented
: > : to -1 and then the question "is -1 >= 0" would be asked and
: > : return false.
: > :
: > : Your second example uses the postfix decrement operator which
: > : behaves as follows...
: > :
: > : The Boolean expression is evaluated, *then* the value of n is
: > : decremented. For example, if n were == 0, the question "is 0 >= 0"
: > : would be asked and return true, *then* n would be decremented
: > : to -1.
: > :
: > : --
: > : Peter [MVP Visual Developer]
: > : Jack of all trades, master of none.
: >
: >
: > <piggybacking>
: >
: >
: > Note that the pre/post decrementation occurs in the context of the
: > boolean test. In both cases, n is decremented prior to entering the
: > logic inside the curly braces. Consider the following C# code:
: >
: > --------------------------------------------------
: > using System;
: >
: > public class Class{
: > public static void Main(){
: >
: > int n = 2;
: > while(--n >= 0){
: > Console.WriteLine("n = " + n);
: > }
: > Console.WriteLine("n when the loop exits: " + n);
: > Console.WriteLine();
: >
: > n = 2;
: > while(n-- >= 0){
: > Console.WriteLine("n = " + n);
: > }
: > Console.WriteLine("n when the loop exits: " + n);
: > }
: > }
: > --------------------------------------------------
: >
: >
: > This is generate the following console output:
: >
: > n = 1
: > n = 0
: > n when the loop exits: -1
: >
: > n = 1
: > n = 0
: > n = -1
: > n when the loop exits: -2
: >
: >
: > In the first case, n is decremented then checked to see if it
: > is >= 0. If so, it is output to the screen. This result in two calls
: > to write to the console:
: >
: > Pass 1: n is decremented to 1
: > n = 1, so the boolean test is true
: > Console.WriteLine is called
: >
: > Pass 2: n is decremented to 0
: > n = 0, so the boolean test is true
: > Console.WriteLine is called
: >
: > Pass 3: n is decremented to -1
: > n = -1, so the boolean test is false
: > The loop is complete
: >
: >
: > In the second case, the test is made to see if n >= 0 and only then
: > is it decremented. This results in three calls to write to the
: > console:
: >
: > Pass 1: n = 2, so the boolean test is true
: > n is decremented to 1
: > Console.WriteLine is called
: > Pass 2: n = 1, so the boolean test is true
: > n is decremented to 0
: > Console.WriteLine is called
: >
: > Pass 3: n = 0, so the boolean test is true
: > n is decremented to -1
: > Console.WriteLine is called
: >
: > Pass 4: n = -1, so the boolean test is false
: > the loop is complete
: >
: >
: > Also, since the value of n is actually being decremented prior to
: > executing the logic that follows the boolean test, translating this
: > into vb is tricky. This is because there isn't a direct
: > corresponding statement in vb to ++ or --. This, imo, is a gross
: > oversite in the language and should be corrected.
: >
: >
: > To achieve the same results as the C# code above, you'll need the
: > following vb logic:
: >
: > --------------------------------------------------
: > Imports System
: >
: > Public Class [Class]
: > Public Shared Sub Main()
: >
: > Dim n As Integer = 2
: > Do Until n < 0
: > n -= 1
: > If n >= 0 Then
: > Console.WriteLine("n = " & n)
: > End If
: > Loop
: > Console.WriteLine("n when the loop exits: " & n)
: > Console.WriteLine()
: >
: > n = 2
: > Do Until n < -1
: > If n >= 0 Then
: > n -= 1
: > Console.WriteLine("n = " & n)
: > Else
: > n -= 1
: > End If
: > Loop
: > Console.WriteLine("n when the loop exits: " & n)
: > End Sub
: > End Class
: > --------------------------------------------------
: >
: >
: > Ugly and unintuitive. And it shouldn't be neccesary - VB should
: > include support for the ++ and -- operators.
: >
: >
: > Ralf
: >
: >