Sign in to follow this  
Followers 0
ripdad

Using a Conditional Switch

3 posts in this topic

#1 ·  Posted (edited)

Recently, I ran into a problem where I wanted a way to

halt the remainder of a script from being executed.

What I wanted was a GOTO :grin:

Found something else that was just as good -- If Not Better!

I'm sure this is not new .. but I think it's pretty neat.

It's a way of executing one single bottom code with a different

set of parameters for each given time or "condition". This saves

having to write duplicating codes and/or scripts.

When the switch is off, it will continue with the top code.

When the switch is on, it will execute the bottom code.

When finished, it will restart from the top.

$switch = 0

$switch = 1

Think of it as an ON/OFF Switch.

Here's an example:

While 1

Sleep(800)

$switch = 0

;

If blah blah

*

*

* <<===== Top Code

*

*

EndIf

;

If @Hour = 15 and @MIN = 30 and @SEC = 00 Then [ Conditional Middle Code ]

$var1 = "c:\blah1\temp.txt"

$var2 = FileOpen($var1, 1)

$var3 = blah

$switch = 1 <<===== Turns ON and Executes bottom code

EndIf

;

If @Hour = 18 and @MIN = 30 and @SEC = 00 Then

$var1 = "c:\blah2\blah3"

$var2 = Run("notepad.exe")

$var3 = blah

$switch = 1 <<===== Turns ON and Executes bottom code

EndIf

;

If $switch = 0 Then ContinueLoop <<===== Restarts loop from the top if not 1

;

$var4 = blah

$var5 = blah

$var6 = blah

;

If blah blah

*

*

* <<===== Bottom Code

*

*

EndIf

WEnd

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites



Where is your question?

Why not using a boolean and a function?

If true then _bla()

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Curious question, what is your previous experience with programming before AutoIt? Was it BASIC?

This saves having to write duplicating codes and/or scripts.

This statement is enough. To save writing duplicate code, we have invented something often referred to as subroutine or function. Here's an example:

The old code:

$a = 4
$b = 5

$c = ($a * 2) + 4
$d = ($b * 2) + 4

If you wanted to change the algorithm, you'd have to change it in 2 places, as well you have to copy the previous code if you want to make new ones. With small algorithms, this is simple, but once it gets larger it becomes more of a problem.

Holy shit! There's functions:

$a = 4
$b = 5

$c = _algorithm($a)
$d = _algorithm($b)

Func _algorithm($x)
    Return ($x * 2) + 4
EndFunc

Awesome. :D

In your script are more typical problems you can solve with functions. For example; Your use $var1, $var2, $var3 which could be parameters for functions. You can also return from a function at any time, which prevents the bottom half from being executed.

Edit: If you could tell us what the application should do, probably simplifying parts of, then we can lay you out a simple process flow of functions.

Edited by Manadar

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  
Followers 0