Jump to content
Sign in to follow this  
Terenz

A For...Next doubt

Recommended Posts

Terenz

Local $One = 1, $Three = 3, $Dinamic

If $Three = 3 Then
    $One = $Dinamic
EndIf

ConsoleWrite("First For...Next: ")
For $x = 1 To 10
    $Dinamic = $x
    ConsoleWrite($One & "-")
Next
ConsoleWrite(@CRLF)

ConsoleWrite("Second For...Next: ")
For $x = 1 To 10
    $Dinamic = $x
    If $Three = 3 Then
        $One = $Dinamic
    EndIf
    ConsoleWrite($One & "-")
Next
ConsoleWrite(@CRLF)

Pratically in my original script if a checkbox was checked a value in the For...Next is different.

The thing i want to avoid is the If...EndIf in the For...Next, but if i declare the variable before the For...Next ( $One = $Dinamic ) it not change in the loop. Any solution or i'm forced to check everytime in the loop the same thing?

Thanks

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
j0kky

I don't know what you do with $One and what happens if $three is different from 3, but in this case it works:

If $Three = 3 Then
    ConsoleWrite("First For...Next: ")
    For $x = 1 To 10
        $One = $x
        ConsoleWrite($One & "-")
    Next
EndIf

Share this post


Link to post
Share on other sites
Terenz

Using your script i need to make two For...Next instead of one

If $Three = 3 Then
    ConsoleWrite("First For...Next: ")
    For $x = 1 To 10
        $One = $x
        ConsoleWrite($One & "-")
    Next
Else
    ConsoleWrite("Second For...Next: ")
    For $x = 1 To 10
        ConsoleWrite($One & "-")
    Next
EndIf

I'd like to make one For...Next without If...EndIf inside

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
Terenz

If useless in my point of view

Check for hundreds of times if $Three = 3 when i know before the loop if is equal or not of that value


Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Share this post


Link to post
Share on other sites
j0kky

I see three possible solutions:

1 you check $three first, but you have to write many loops as values $three can have

2 you check $three into the loop

3 you work with memory addresses instead of simple variables... But this is a madness.

EDIT:

I've made a test script and I discovered something I never expected: the first approach is 2,38x faster than the second! Obviously I considered millions of cycles, you can't ever notice the difference with a normal script...

Local $a = 0, $b = 0, $c = 0, $timer = TimerInit()
While $b < 100000
    For $i = 0 To 10
        If $a = 0 Then
            $c += 1
        Else
            $c += 2
        EndIf
    Next
    $b += 1
WEnd
$diff1 = TimerDiff($timer) / 100000

Local $a = 0, $b = 0, $c = 0, $timer = TimerInit()
While $b < 100000
    If $a = 0 Then
        For $i = 0 to 10
            $c += 1
        Next
    Else
        For $i = 0 To 10
            $c += 2
        Next
    EndIf
    $b += 1
WEnd
$diff2 = TimerDiff($timer) / 100000

ConsoleWrite($diff1 & @CRLF & $diff2 & @CRLF & ($diff1 / $diff2) & @CRLF)
Edited by j0kky

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×