# [Solved] For (If Then Else) Next loops fail

## Recommended Posts

Hello,

I'm searching and searching but can't find the solution. I wan't to make a for next loop like this:

```\$p = 0

For \$n = 1 To 5

MsgBox(4096, "", \$p)

If \$p = 0 Then MsgBox(4096, "", "IF")

\$p = 1

Else MsgBox(4096, "", "ELSE")

\$p = 0

EndIf

Next```

AU3Check means

in line 2 REF: missing Next

in line 6 ERROR: missing next / ERROR: syntax error

Is there no way to have a "If Then Else" loop inside a "For Next" loop?

Regards, Simpel

Edited by Simpel
solved

```SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)```

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

##### Share on other sites

If you put an action on the end of an If/Then statement it terminates the If/Then statement, making it a single-line If/Then and any following Else or EndIf is not treated as part of that If/Then statement.

Just move the Msgbox() down to a separate line beneath the If/Then, leaving the If/then statement ending with the word "Then"

Edit: PS - Welcome to the forum!

Edited by Spiff59

##### Share on other sites

Hi, Simpel, welcome to the forum. You need to fix your If/Then lines. Take a look at this to see the difference.:

```\$p = 0

For \$n = 1 To 5
MsgBox(4096, "", \$p)
If \$p = 0 Then
MsgBox(4096, "", "IF")
\$p = 1
Else
MsgBox(4096, "", "ELSE")
\$p = 0
EndIf
Next```

Edit: Man I am slow today

Edited by JLogan3o13

√-1 2^3 ∑ π, and it was delicious!

##### Share on other sites

It's just as simpel as that.

Thanks for the very fast help.

Regards, Simpel

```SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)```

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

##### Share on other sites

√-1 2^3 ∑ π, and it was delicious!

## Create an account

Register a new account

×

• Wiki

• Back

• Git