Results 1 to 3 of 3

Thread: using expressions outside the loop

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    4

    using expressions outside the loop

    Hi there, here is my problem:
    I need expressions for time(n) so that i can calculate time(n) in a for loop(for k=1 to 100000). i don't know the expressions, so the code has to do it for me:
    Code:
    For i As Integer = 1 To 26
                    For j As Integer = 0 To i - 1
                        If RowsCols(i)(4) - RowsCols(j)(4) = RowsCols(j)(2) And
                            Then
                            time(i) = time(j) + t(i)
                            Exit For
                        End If
                    Next
                Next
    by this, i get 26 expressions for time(n) (from time(1) to time(26)). the thing is, i don't want to do this in the for loop of k=1 to 100000, which is awaste of time, i want to do it outside the loop. but i want to use the result- the expression for time(n)- in the for loop of k=1 to 100000.
    is it possible?? if yes, how to use simply the expression in the for loop? i have no idea what kind of problem this is, so couldn't find any result by google search. Thanks in advance!

  2. #2
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,570

    Re: using expressions outside the loop

    I don't know that I understand what you are asking. Is the snippet you posted just an example of 26 iterations when what you want to actually do would be the same loop 100,000 times? If so, then there is no better way to do it than what you are doing based on what you showed and a few assumptions. You might use LINQ, which would look smaller, but would actually take longer.

    However, there are some flaws in that example that are puzzling. For one thing, there is a stray And in there with the Then for the If statement showing up on the next row. A greater issue is that the inner line is this:

    time(i) = time(j) + t(i)

    There may well be a way to make this more efficient if there is any way to predict when the If statement will be true. If you can do that, it may be possible to calculate time(i) without performing any loops at all. However, we lack sufficient information to even know what that condition is, thanks in part to the strange And, but also largely because we have no idea what you are trying to accomplish. What is RowsCols? What is in there? What it t? Is there a way to predict when the condition will be true?
    My usual boring signature: Nothing

  3. #3
    New Member
    Join Date
    Aug 12
    Posts
    4

    Re: using expressions outside the loop

    Thanks Shaggy.
    Yes, the snippet would be the same if i put it in loop 100,000 times, and that's why i think it's useless to repeat it 99,999 more times other than read it just for one time. if only i can put it outside the loop!
    And yeah, there shouldn't be an 'and'. (was trying to make my snippet look a bit simpler so that i cut off some part but left the 'and' out)
    those RowsCols are actually from an Excel file from which i read data from. there are 27 events listed in a column, and each of them starts when some event finishes. the first event in the column will happen the first, but all the rest will not just happen in the order as they are listed. i need to know the order they start, so that i can calculate the time each event finishes, thus the time all of them finishes(repeat 100000 times). The snippet here is how i determine the order.

Posting Permissions

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