Results 1 to 4 of 4

Thread: For loop

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    1

    For loop

    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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,395

    Re: For loop

    Well take a look at the structure of a basic for loop:
    Code:
    For counter [ As datatype ] = start To end [ Step step ]
    
    Next
    For - Starts the loop
    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:
    Code:
    For i As Integer = 1 To 10
    
    Next
    To count backwards we include the Step, which will be after the 'To end'.
    Code:
    For i As Integer = 11 To 2 Step -1
    
    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 is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: For loop

    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

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,395

    Re: For loop

    Quote Originally Posted by .paul. View Post
    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
    Ahh... I didn't interpret the question that way. That makes more sense.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width