I am doing a program at school, it requires me to have a for loop, it need to count from 1 to 10 but it also needs it count it down from 11 to 2. Could someone tell me how to do this? Thanks
Printable View
I am doing a program at school, it requires me to have a for loop, it need to count from 1 to 10 but it also needs it count it down from 11 to 2. Could someone tell me how to do this? Thanks
Well take a look at the structure of a basic for loop:
For - Starts the loopCode:For counter [ As datatype ] = start To end [ Step step ]
Next
counter - The control variable for that loop
[As DataType] - the datatype will be a numerical datatype
= start - Set the start value of the counter
To end - Where do you want it to end at
So a for...next loop to count from 1-10 would look like this:
To count backwards we include the Step, which will be after the 'To end'.Code:For i As Integer = 1 To 10
Next
The negative 1 says that we will decrease by 1, if it was -2 then it'd decrease by 2, if it was positive 1 then it'd increase by 1.Code:For i As Integer = 11 To 2 Step -1
Next
try this. x is the 1 to 10 loop variable, + y is the 11 to 2 variable:
Code:For x As Integer = 1 To 10
Dim y As Integer = 11 - (x - 1)
Next