Jump to content

Do Until - [ Loops ]


Recommended Posts

Need a little bit of help please, I'm afraid I am one of those people who used to use lots of Gotos etc in their scripts and I want to change my ways.

With the previous version of Autoit I wrote some small apps which aided a team into moving and updating a anti virus package. Now I want to incorpate all these small programs into one with a menu system.

I have got as far as the first into message box etc but I am stuck in regards to a Input box asking the user to decide what part of the program to run.

There will be 3 or 4 possible choices - what they enter will be a string but how do I loop the box until someone has entered a choice ? This is as far as I have got.

;Display Message Box

Dim $RC

$RC = MsgBox (4, "Computer Lab. Written By Matt. March 2004", "Collection of Programs PC Support administrators. Note - Force update of AV Pattern file will detect which server the client reports to. Continue ?")

;Displays the value MsgBox (0, "answer", $RC)

If $RC = 7 Then Exit

;Display Input Box

Dim $Exit

[ I thought declaring a variable $Exit may help if I did a DoUntil, would a DoUntil $Exit = N help ? ]

My knowledge on looping little things like this is pratically non-exsistant so would apprecaite some help. If anyone can point me to a website that has a dummys guide, then even better !!

After that I plan a input box or does anyone think a Hotkey list would be better giving the user the choice of what part of the program needs running ?

Link to comment
Share on other sites

  • Developers

this example loops till you type something or hit the cancel button:

$CHOICE = ""
While $CHOICE = ""
   $CHOICE  = InputBox("Choice", "Choice?", "", "", -1, -1, -1, -1)
   If @error = 1 Then; cancel clicked
      Exit
   EndIf
Wend
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

There are many ways to do it, normally I loop the msgbox til I get the answer I like, and then continue.

While 1
$var = InputBox("Choice", "Choice?", "", "", -1, -1, -1, -1)
if $var<>"" then exitloop
wend

Select
   Case $var = 1
       MsgBox(0, "", "First Case exp[b][/b]ression was true")
   Case $var = "test"
       runthis()
   Case Else
       MsgBox(0, "", "No preceding case was true!")
EndSelect

func runthis()
MsgBox(0, "", "Second Case exp[b][/b]ression was true")
endfunc

JdeB I think you wanted $SUSSERVER to be $CHOICE? This doens't work well for me.

$CHOICE = ""

While $CHOICE = ""

  $SUSSERVER = InputBox("Choice", "Choice?", "", "", -1, -1, -1, -1)

  If @error = 1 Then; cancel clicked

  Exit

  EndIf

Wend

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Thanks for the quick replies - I am trying to go though in my head the examples of code you have provided - bit hard going though it step by step - [ still learning slowly, this new version of Autoit really is hard to grasp for a simpleton like me !! ]

Link to comment
Share on other sites

  • Developers

ScriptKitty, yea you're correct... did a copy&paste from a SUSscript and forgot to modify all var names... tnx

Mattx:

Let me run you trough the example i gave:

; initialise var $CHOICE to ""
$CHOICE = ""
; keep looping this while..wend until $CHOICE has a different value
While $CHOICE = ""
 ; prompt for input ...
  $CHOICE  = InputBox("Choice", "Choice?", "", "", -1, -1, -1, -1)
  ; when cancel is hit the @error will contain a value of 1 else 0
  If @error = 1 Then; cancel clicked
     Exit
  EndIf
Wend
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks - that explains it a little better - I have never had any proper teaching - most of my stuff is self taught so I appreciate the explanations.

I did do a VB6 course a while back but that did not really do what I wanted to do and it was more geared towards databases etc.

Dunno if its me but most of my stuff has been mainly batch and earlier versions of Autoit written - this is a large step for me and I am finding it a little hard in places........

Link to comment
Share on other sites

  • 2 weeks later...

Using an pre-test loop like While when you do not know the value to be tested until you are inside the loop is a bit of a cludge. Use a post-test loop like Do-Until instead.

; initialise var $CHOICE to ""
$CHOICE = ""
; keep looping this while..wend until $CHOICE has a different value
While $CHOICE = ""
; prompt for input ...
 $CHOICE  = InputBox("Choice", "Choice?", "", "", -1, -1, -1, -1)
; when cancel is hit the @error will contain a value of 1 else 0
 If @error = 1 Then; cancel clicked
    Exit
 EndIf
Wend

Becomes

; Start loop
Do
  ; prompt for input ...  (all the default value markers are not needed)
   $CHOICE  = InputBox("Choice", "Choice?")
  ; when cancel is hit the @error will contain a value of 1 else 0
   If @Error = 1 Then Exit; cancel clicked (one-line If)
; Stay in loop unless (until) $CHOICE is not blank
Until $CHOICE > ""

Test when you need to test and do not be afraid to try new things, like other looping constructs.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...