By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,556 Members | 1,669 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,556 IT Pros & Developers. It's quick & easy.

More C# to VB

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
Hi there.. The prefix operator "increments" the variable value before
iterating, postfix in the other hand does exactly the same but when the loop
returns.

Regards,
--
Angel J. Hernández M.
MCP - MCAD - MCSD - MCDBA
http://groups.msn.com/desarrolladoresmiranda

"OpticTygre" <op********@adelphia.net> escribió en el mensaje
news:g4********************@adelphia.com...
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.

Nov 21 '05 #2

P: n/a

"OpticTygre" <op********@adelphia.net> wrote in message
news:g4********************@adelphia.com...
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.
Nov 21 '05 #3

P: n/a

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:Oz****************@tk2msftngp13.phx.gbl...
:
: "OpticTygre" <op********@adelphia.net> wrote in message
: news:g4********************@adelphia.com...
:
: >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
Nov 21 '05 #4

P: n/a
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.

-Jason

"_AnonCoward" <ab*@xyz.com> wrote in message
news:he*********************@twister.southeast.rr. com...

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:Oz****************@tk2msftngp13.phx.gbl...
:
: "OpticTygre" <op********@adelphia.net> wrote in message
: news:g4********************@adelphia.com...
:
: >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

Nov 21 '05 #5

P: n/a

"OpticTygre" <op********@adelphia.net> wrote in message
news:vq********************@adelphia.com...
:
: 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
:
: "_AnonCoward" <ab*@xyz.com> wrote in message
: news:he*********************@twister.southeast.rr. com...
: >
: > "Peter van der Goes" <p_**********@toadstool.u> wrote in message
: > news:Oz****************@tk2msftngp13.phx.gbl...
: > :
: > : "OpticTygre" <op********@adelphia.net> wrote in message
: > : news:g4********************@adelphia.com...
: > :
: > : >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
: >
: >


Nov 21 '05 #6

P: n/a
Yeah, I see your point. This isn't the only place things like that can
happen. It all goes back to the issue that assignments aren't valid within
expressions in VB code. Consider how confusing things become when trying to
translate something like:

while (((symbol = litlenTree.GetSymbol(input)) & ~0xff) == 0) {
do some code
}

Fun stuff. :o)

"_AnonCoward" <ab*@xyz.com> wrote in message
news:iJ*********************@twister.southeast.rr. com...

"OpticTygre" <op********@adelphia.net> wrote in message
news:vq********************@adelphia.com...
:
: 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
:
: "_AnonCoward" <ab*@xyz.com> wrote in message
: news:he*********************@twister.southeast.rr. com...
: >
: > "Peter van der Goes" <p_**********@toadstool.u> wrote in message
: > news:Oz****************@tk2msftngp13.phx.gbl...
: > :
: > : "OpticTygre" <op********@adelphia.net> wrote in message
: > : news:g4********************@adelphia.com...
: > :
: > : >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
: >
: >

Nov 21 '05 #7

P: n/a
Hi Ralf,

"_AnonCoward" <ab*@xyz.com> wrote in message
news:he*********************@twister.southeast.rr. com...

"Peter van der Goes" <p_**********@toadstool.u> wrote in message
news:Oz****************@tk2msftngp13.phx.gbl...
:
: "OpticTygre" <op********@adelphia.net> wrote in message
: news:g4********************@adelphia.com...
:
<snip>
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


Ralf, why should a language derived from BASIC (created in 1964 or 65 IIRC),
which never had prefix nor postfix operators, implement such a thing now?
BTW, how many other languages not derived from C (created in 1971 or 72
IIRC) have prefix and or postfix operators?

Thanks,

Charles
Nov 21 '05 #8

P: n/a
"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
<snip>

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:


Ralf, why should a language derived from BASIC (created in 1964 or 65
IIRC), which never had prefix nor postfix operators, implement such a
thing now? BTW, how many other languages not derived from C (created in
1971 or 72 IIRC) have prefix and or postfix operators?


The same reason they implemented case expressions, classes/objects and
hundreds of other features "borrowed" from other languages. Allegedly these
things make the life of the programmer easier.
Nov 21 '05 #9

P: n/a

"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
:
: Hi Ralf,
Greetings.
<snip>
: > VB should include
: > support for the ++ and -- operators.
: >
: >
: > Ralf
: >
:
: Ralf, why should a language derived from BASIC (created in 1964 or
: 65 IIRC), which never had prefix nor postfix operators, implement
: such a thing now? BTW, how many other languages not derived from C
: (created in 1971 or 72 IIRC) have prefix and or postfix operators?
:
: Thanks,
:
: Charles
Why shouldn't it? VB isn't BASIC anymore than C# is C. It is an
outgrowth from that language that has seen huge changes over the
decades. As it has grown and matured, new features have been added (as
just one example, V1.0 of the vb.net compiler didn't support the << or
bit operators but V1.1 does). I see no reason to preclude adding the

++/-- operators, or any other new feature, to the language just because
of its historical relationship to an older language.
Ralf
Nov 21 '05 #10

P: n/a
Hi Michael,

"Michael C#" <xy*@abcdef.com> wrote in message
news:Xs*****************@fe10.lga...
"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
<snip>

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:


Ralf, why should a language derived from BASIC (created in 1964 or 65
IIRC), which never had prefix nor postfix operators, implement such a
thing now? BTW, how many other languages not derived from C (created in
1971 or 72 IIRC) have prefix and or postfix operators?


The same reason they implemented case expressions, classes/objects and
hundreds of other features "borrowed" from other languages. Allegedly
these things make the life of the programmer easier.

OK, wrt select case, classes, et al, I see the advantages; but I do not see
any advantage to implementing a prefix or postfix increment or decrement
operator syntax in the context of a BASIC derived language. It is easier for
me to read and understand explicit increment/decrement before or after
further use in an expression. That's just my preference, and I am not a
compiler writer :).

Thanks,

Charles

PS, does anyone know if any other programming language uses C's
prefix/postfix increment/decrement operators?
Nov 21 '05 #11

P: n/a
Hi Ralf,

"_AnonCoward" <ab*@xyz.com> wrote in message
news:dd*********************@twister.southeast.rr. com...

"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:eH*************@TK2MSFTNGP15.phx.gbl...
:
: Hi Ralf,
Greetings.
<snip>
: > VB should include
: > support for the ++ and -- operators.
: >
: >
: > Ralf
: >
:
: Ralf, why should a language derived from BASIC (created in 1964 or
: 65 IIRC), which never had prefix nor postfix operators, implement
: such a thing now? BTW, how many other languages not derived from C
: (created in 1971 or 72 IIRC) have prefix and or postfix operators?
:
: Thanks,
:
: Charles
Why shouldn't it? VB isn't BASIC anymore than C# is C. It is an
outgrowth from that language that has seen huge changes over the
decades. As it has grown and matured, new features have been added (as
just one example, V1.0 of the vb.net compiler didn't support the << or
bit operators but V1.1 does). I see no reason to preclude adding the
++/-- operators, or any other new feature, to the language just because
of its historical relationship to an older language.
Ralf

That I understand (and yes, I would have liked having the shift operators in
VB.NET 2002); but I don't see the advantage of having them relative to the
ease of introducing bugs by misusing them when you forget exactly what is
happening to the variable when you are using the construct. If
prefix/postfix increment/decrement operators get added to the language, I
won't object at all.

Thanks,

Charles

Nov 21 '05 #12

P: n/a

"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
OK, wrt select case, classes, et al, I see the advantages; but I do not
see any advantage to implementing a prefix or postfix increment or
decrement operator syntax in the context of a BASIC derived language. It
is easier for me to read and understand explicit increment/decrement
before or after further use in an expression. That's just my preference,
and I am not a compiler writer :).
Hi Charles, just because a feature is present in a language does not mean
you absolutely have to use it. For any given program, there are probably
hundreds of functions, classes, keywords, etc. that you ignore while coding.
They are nice to have around when you need them however.

Do the +=, -=, *= and /= operators in VB.NET bother you also? They found
their way from C-style languages into VB.NET also... Apparently someone
thought they might be useful and included them. However, you are by no
means forced to use them. Ditto for the ++ and -- prefix and postfix
operators in C, C++, C#, Java or any other language that has them. It would
be just another tool in the toolbox, and if you didn't like it, you wouldn't
have to use it...

Seriously though, does this summarize the argument: prefix and postfix ++
and -- operators should be kept out of VB.NET in 2005 because they were not
part of some 40 year old computer language that happens to share a couple of
keywords with the current version of VB.NET? If so, as mentioned, they need
to roll back a *lot* of functionality to the pre-1970 era mish-mash of COBOL
+ FORTRAN + et al., that was called "BASIC".
PS, does anyone know if any other programming language uses C's
prefix/postfix increment/decrement operators?


Perl. PHP. Java. And a whole host of modern languages also implement the
+= and -= operators.
Nov 21 '05 #13

P: n/a
Hi Michael,

"Michael C#" <xy*@abcdef.com> wrote in message
news:Vg******************@fe10.lga...

"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:uB**************@tk2msftngp13.phx.gbl...
OK, wrt select case, classes, et al, I see the advantages; but I do not
see any advantage to implementing a prefix or postfix increment or
decrement operator syntax in the context of a BASIC derived language. It
is easier for me to read and understand explicit increment/decrement
before or after further use in an expression. That's just my preference,
and I am not a compiler writer :).
Hi Charles, just because a feature is present in a language does not mean
you absolutely have to use it. For any given program, there are probably
hundreds of functions, classes, keywords, etc. that you ignore while
coding. They are nice to have around when you need them however.

Agreed
Do the +=, -=, *= and /= operators in VB.NET bother you also? They found
their way from C-style languages into VB.NET also... Apparently someone
thought they might be useful and included them. However, you are by no
means forced to use them. Ditto for the ++ and -- prefix and postfix
operators in C, C++, C#, Java or any other language that has them. It
would be just another tool in the toolbox, and if you didn't like it, you
wouldn't have to use it...
Actually, +=, et al don't bother me. It is a shortcut for var = var + rhs,
etc.
Seriously though, does this summarize the argument: prefix and postfix ++
and -- operators should be kept out of VB.NET in 2005 because they were
not part of some 40 year old computer language that happens to share a
couple of keywords with the current version of VB.NET? If so, as
mentioned, they need to roll back a *lot* of functionality to the pre-1970
era mish-mash of COBOL + FORTRAN + et al., that was called "BASIC".

I am not saying that prefix ++ and postfix ++ should be kept out of the
language. I am asking for an explanation of the rationale to include them if
another syntactical form is available that makes it clearer just what is
being done. I do not want to go back to 60's/70's programming styles. I
dropped classic BASIC and readily moved to VB.NET. Fortran 95 isn't classic
FORTRAN, but it is about where VB6 was. Fortran will be an OO language when
the next compiler standard is released (and it will not be your father's
FORTRAN). There are times when you can have too much terseness, just as
there are times when you can have too much verbosity. I am looking for
balance. :) You can probably tell that BASIC and FORTRAN were the first two
computer languages that I learned.
PS, does anyone know if any other programming language uses C's
prefix/postfix increment/decrement operators?


Perl. PHP. Java. And a whole host of modern languages also implement
the += and -= operators.

I was aware of Java, but not Perl nor PHP. Thanks :)

Charles
Nov 21 '05 #14

P: n/a
"Charles D. Quarles" <cd*******@msn.com> wrote in message
news:u0*************@TK2MSFTNGP15.phx.gbl...
I am not saying that prefix ++ and postfix ++ should be kept out of the
language. I am asking for an explanation of the rationale to include them
if another syntactical form is available that makes it clearer just what
is being done.
++ and -- were originally implemented as 'hints' to the compilers way back
in the day, to help them optimize code (they were horrible at optimization).
You can actually use ++x similarly to x += 1 or x = x + 1. Just consider it
shorthand. The advantage to using this syntax is that you can use ++ in
various conditionals to compare and then automatically update "x++" (or
automatically update and then compare "++x"), where with x += 1 or x = x + 1
you have to have at least two statements to do the same thing. Again, you
could avoid this type of usage and just use it as shorthand for x += 1, but
it is handy when you decide you want to use it in a conditional or other
statement.
I do not want to go back to 60's/70's programming styles. I dropped
classic BASIC and readily moved to VB.NET. Fortran 95 isn't classic
FORTRAN, but it is about where VB6 was. Fortran will be an OO language
when the next compiler standard is released (and it will not be your
father's FORTRAN). There are times when you can have too much terseness,
just as there are times when you can have too much verbosity. I am
looking for balance. :) You can probably tell that BASIC and FORTRAN were
the first two computer languages that I learned.


BASIC is, by definition, verbose. Some people don't notice it because VS
automatically fills out half the keywords for you; but they are there. The
main thing about ++ and -- is not just that they are shorthand for += 1, but
they provide additional functionality not available in BASIC without writing
your own functions to do the job.
PS, does anyone know if any other programming language uses C's
prefix/postfix increment/decrement operators?


Perl. PHP. Java. And a whole host of modern languages also implement
the += and -= operators.

I was aware of Java, but not Perl nor PHP. Thanks :)


There's quite a few languages that are either "C-style" or have implemented
these particular operators. These were just a few that I'm familiar with.
Nov 21 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.