VB.NET - Grouping statements on one line - vb.net

I want to put several statements on one line, including conditional statements that need to be grouped together.
I can do this:
x += 20 : y += 10 : If x > 400 Then x = 0
I want to have more than one statement under 'if'. It balks when I try to do this:
x += 20 : y += 10 : (If x > 400 Then x = 0 : y = 0)
or this:
x += 20 : y += 10 : If x > 400 Then (x = 0 : y = 0)
Is there a way?

x += 20 : y += 10 : If x > 400 Then x = 0 : y = 0
But I wouldn't recommend putting it on one line. It's a bit nasty to readability.

Related

Excel VBA Loop x = x + 1

I'm new to Excel VBA and i'm trying to make a loop that sums X = X + 1 but when the loop ends it continues with the last X and doesn't starts again.
This is what I have:
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
Debug.Print K
Next K
Next I
This is what i get: 1 2 3 4 1 2 3 4 1 2 3 4 .
What i would like to get is: 1 2 3 4 5 6 7 8 9 10 11 12 .
Thanks for the help provided. I thought this would solve my problem but it's a bit more complicated. I need this because i'm adding coordinates in X, Y, Z format with this code:
For I = 1 To 6
X = 0
J = 10
RobApp.Project.Structure.Nodes.Create X = X + 1, 0, 0, J * (I - 1)
RobApp.Project.Structure.Nodes.Create X = X + 1, Range("N34") * 0.15, 0, J *
(I - 1)
Next I
"X = X+1" is the node number. I want it to be sequencial, 1,2,3,4 and so on while J is increasing in the Z coordinate. For example for the first line of code:
Node 1 = 0,0,0
Node 2 = 0,0,10
Node 3 = 0,0,20
and so on!
Or rather, use the extra variable X as you originally planned:
X = 0
For I = 1 To 3
J = 2
For K = 1 To J * 2 Step 1
X = X + 1
Debug.Print X
Next K
Next I

Trying to make a For Loop that adds something in Visual Basic; not getting right answer

I need to make a console app for a class, and it has to compute the following using a For Next loop: 4 + 8 + 12 + 16 + 20 .... + 208
Here's what I have:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + 4
Console.WriteLine(z)
Next
I have no idea what I'm doing wrong.
On each iteration, you are adding 4 to z, so you are actually computing 4 + 4 + 4 + ... + 4. What you really want to do is to add x to z:
Dim x As Integer = 0
Dim z As Integer = 4
For x = 0 To 208 Step 4
z = z + x
Console.WriteLine(z)
Next

what to do with fraction as index number

given these inputs x = 4, S = [1 2 3 4 5 6 7 8 9 10], and n = 10
search (x,S,n) {
i = 1
j = n
while i < j {
m = [(i+j)/2]
if x > Sm then i=n+1
else j = m
end
if x = Si then location = i
else location = 0
This code is not from any particular language its just from my discrete math hw, but I'm confused as to what Sm would equal on the first iteration because m would be 11/2. If i use a fraction as the index do I round down? Is there a general rule for this? Am I making any sense? Help pls

What does += mean in Visual Basic?

I tried to google the answer for this but could not find it. I am working on VB.Net. I would like to know what does the operator += mean in VB.Net ?
It means that you want to add the value to the existing value of the variable. So, for instance:
Dim x As Integer = 1
x += 2 ' x now equals 3
In other words, it would be the same as doing this:
Dim x As Integer = 1
x = x + 2 ' x now equals 3
For future reference, you can see the complete list of VB.NET operators on the MSDN.
a += b
is equivalent to
a = a + b
In other words, it adds to the current value.
It is plus equals. What it does is take the same variable, adds it with the right hand number (using the + operator), and then assigns it back to the variable. For example,
Dim a As Integer
Dim x As Integer
x = 1
a = 1
x += 2
a = a + 2
if x = a then
MsgBox("This will print!")
endif
those 2 lines compiled produce the same IL code:
x += 1
and
x = x + 1
Just makes code more efficient -
Dim x as integer = 3
x += 1
'x = 4
is the same as
x = x + 1
'x = 4
It can also be used with a (-):
x -= 1
' x = 2
Is the same as
x = x - 1
'x = 2

Iterations In From Loop

is there a way to say in vba something like:
from x = 1 to 100, by 10
so that the x's are 1, 10, 20, 30, etc. to 100?
You can use STEP:
for x = 0 to 100 step 10
next x
This will take you through 0, 10, 20... 100
Since you want to start at 1 and go 1, 10, 20... 100, here is a slight modification
for x = 0 to 100 step 10
if x = 0 then
y = 1
else
y = x
end if
'// use y in all calculations downstream instead of x
next x
For y = 0 To 10
If y = 0 Then x = 1 Else x = 10 * y
' do stuff with x
Next y