Jump to content

Exit If in while and restart while


geocast
 Share

Recommended Posts

Hi everyone

I've tried searching but haven't found anything.

I have a while loop with a couple of If statements. Now when one of these If statements is done, I want it to exit the If's and start the while loop from the beginning.

Here is a part of my code, that I want to add this functionality

While 1
   $h += 1
   $WAnhang = _ExcelReadCell ($oExcel, $h, 2)
   If @error Then
      MsgBox(0, "Error sending message", "Error code:" & @error )
      ExitLoop
      
   ElseIf _ExcelReadCell($oExcel, $h, 1) = "" Then 
      MsgBox(1,"Meldung", "Fertig")
      ExitLoop
   ElseIf $WAnhang = 1
      $s += 1
      $datensatz = _ExcelReadCell ($oExcel, $h, 1)
      _ExcelWriteArray($nExcel, $s, 1, $datensatz, 1)

After the last ElseIf statement I want it to restart from while.

Thanks for your help!

Link to comment
Share on other sites

Welcome to AutoIt and the forum!

Your code does exactly what you are asking for. No need to change.

Edit: Missed the ExitLoop statemsnts. They are not needed. Remove them.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It looks like you need to end your While loop with a WEnd.  Also, ExitLoop will exit your While loop, so remove those , add a WEnd after your IF statements and give it a try.  Also, an EndIf seems to be in order.

Edited by aleph01

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

Thanks. Have read a lot here, but now was a little stumped.

I should have mentioned, that the code carries on. There are a couple more If statement after the given one. But for them to work correctly this one has to exit and restart.

I'm aware of the missing WEnd and so on. I've only posted that part of the code. Sorry for not explaning that.

So basicly I only have to end that statement with EndIf in it and it will exit and restart and do the If statements again?

Link to comment
Share on other sites

If your EndIf is followed by WEnd, yes, you will start the While loop again.  If you have other statements after your EndIf, they will be executed.  If you have more questions or run into additional difficulties, post your entire code so forum members can give you more accurate answers.  By the way, you might want to put a short Sleep in your while loop to keep it from eating up your processor time.

Edited by aleph01

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

This is the entire code

Dim $h = 0
Dim $s = 0
$nExcel = _ExcelBookNew(0)
$oExcel = _ExcelBookOpen("C:\Users\Stefan\Desktop\Datenbank.xls", 0)

While 1
   $h += 1
   $WAnhang = _ExcelReadCell ($oExcel, $h, 2)
   If @error Then
      MsgBox(0, "Error sending message", "Error code:" & @error )
      ExitLoop
      
   ElseIf _ExcelReadCell($oExcel, $h, 1) = "" Then 
      MsgBox(1,"Meldung", "Fertig")
      ExitLoop

 1.  ElseIf $WAnhang = 1 Then

      $s += 1
      $datensatz = _ExcelReadCell ($oExcel, $h, 1)
      MsgBox(1, "test", $datensatz)
      _ExcelWriteArray($nExcel, $s, 1, $datensatz, 1)

         ;exit IF and restart
      
 2.  ElseIf $WAnhang <> 1 Then
      
      ;Do something
      

      
   EndIf
WEnd

If the 1. statement (just commented it to explain easier) should be executed, it should restart from while. That the next case can be handled with the 2. statement.

Link to comment
Share on other sites

Someone correct me if I'm wrong, I learning scripting, too.  In an If, ElseIf, Else, EndIf statement, if the IF is true, the rest of the ElseIfs and Else (if used) are ignored and the script goes immediately to the EndIf.  If the If statement is not true, the first ElseIf is read.  If it is true, those commands are executed and  the script goes to EndIf.  If none of your If or ElseIf statements are true, the Else statement is executed as a default (if you use an Else statement).  If you don't use an Else statement and none of your If or ElseIf statements are true, no action is taken and the script starts running the line after EndIf.  This can make the order of your ElseIf statements important, since the first one that is true is going to be executed and the rest ignored. 

Only keep your ExitLoop statements if you intend to quit the While loop, and put a Sleep(50) or longer in your While loop (not in your If or ElseIf statements) to keep your loop from hogging your processor.  Often the Sleep is the  last statement in the While loop, but it isn't necessary that it be last.  It could be first, for example.

Edited by aleph01

Meds.  They're not just for breakfast anymore. :'(

Link to comment
Share on other sites

Unless I'm misreading, it looks like you need a "ContinueLoop" where you have the comment ";exit IF and restart". 

EDIT: I should amend what I said and instead say "it looks like this is the control condition that satisfies your question".

E.g.

Dim $h = 0
Dim $s = 0
$nExcel = _ExcelBookNew(0)
$oExcel = _ExcelBookOpen("C:\Users\Stefan\Desktop\Datenbank.xls", 0)

While 1
   $h += 1
   $WAnhang = _ExcelReadCell ($oExcel, $h, 2)
   If @error Then
      MsgBox(0, "Error sending message", "Error code:" & @error )
      ExitLoop
      
   ElseIf _ExcelReadCell($oExcel, $h, 1) = "" Then 
      MsgBox(1,"Meldung", "Fertig")
      ExitLoop

 1.  ElseIf $WAnhang = 1 Then

      $s += 1
      $datensatz = _ExcelReadCell ($oExcel, $h, 1)
      MsgBox(1, "test", $datensatz)
      _ExcelWriteArray($nExcel, $s, 1, $datensatz, 1)

         ContinueLoop()
      
 2.  ElseIf $WAnhang <> 1 Then
      
      ;Do something
      

      
   EndIf
WEnd
Edited by mrider

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Link to comment
Share on other sites

I amended my question for a simple reason: As aleph01 correctly points out, the code inside "ElseIf $WAnhang = 1 then" will execute (assuming WAnhang == 1) and then the loop will continue anyway.

Not every programming language works like this, but the overwhelming majority of programming languages work their way through if and case statements until the first match is found and then the code inside that section are run.  If a section of code is run, then no more sections are run or even checked - even if they might also satisfy the condition.  An "else" (or equivalent) section is only run if no matches are found.

 

There are a few languages (although I don't know any offhand) that test ALL conditions in the section, and so you can have errors in odd places.  For example, the following pseudo code might break in some really esoteric languages:

If foo <> null and foo == "bar"

The reason is that some languages will test the 'foo == "bar"' part, even if it doesn't make sense.  But that's quite rare.

Edited by mrider

How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

Link to comment
Share on other sites

Not reading all the threads, but replace the exit loops with continueloop, to skip the remainder of the loop, and start it fresh

ContinueLoop

Else, you would need to create another while loop outside of your current loop, so the exit will go back to the wrapping loop, to re-enter the inner (which has all your code)

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
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...