USA
Asked — Edited

Elseif Is Broken

@DJ,

The ElseIf is broken:

The code below never stops !


$buffer="[123 456 789]"
$len=Length($buffer)
$ix=0
$start=-1
$end=-1
repeatwhile($ix<$len)
  $ch=GetCharAt($buffer, $ix)
  if ($ch="[")
    print("found start")
    $start=$ix
  elseif ($start>=0 AND $ch="]")
    print("found end")
    $end=$ix
    # break
    $ix=$len+1
  endif
  print("increment ix")
  $ix=$ix+1
endrepeatwhile
print($ix)
print($start)
print($end)

if you remove the ElseIf it works as expected.


$buffer="[123 456 789]"
$len=Length($buffer)
$ix=0
$start=-1
$end=-1
repeatwhile($ix<$len)
  $ch=GetCharAt($buffer, $ix)
  if ($ch="[")
    print("found start")
    $start=$ix
  endif
  if ($start>=0 AND $ch="]")
    print("found end")
    $end=$ix
    # break
    $ix=$len+1
  endif
  print("increment ix")
  $ix=$ix+1
endrepeatwhile
print($ix)
print($start)
print($end)

This bug must be recent, i've used before the ElseIf, or did i miss something ?


ARC Pro

Upgrade to ARC Pro

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

#1  

Does the ElseIf require its own EndIf?

This is one thing I've never been clear ablout EZ Script as other languages do require that but EZ Script doesn't seem to require it

PRO
USA
#2  

@faengelm:

No. Only one Endif for the If clause.

Tutorial: https://synthiam.com/Community/Tutorials/134/1

Quote:

other languages do require

Some languages have optional code blocks and don't need the endif: C, C#, Java { } TSQL: Begin ... End

Python uses indentation and sometimes is a pin a neck (tab vs spaces):


if expression1:
   statement(s)
elif expression2:
   statement(s)
elif expression3:
   statement(s)
else:
   statement(s)

Curiosity, what program language do you have in mind that requires 2 endifs ?

#3  

@ptp You are right. I was confused by the case where instead of an ElseIf there is an Else with a nested If ... Endif

#4  

Hi @PTP I tried the simplest

If ElseIF EndIf

I could think of and this is working both trying $a=1 and $a=2


$a=2

:loop
if ($a=1)
  Print( "$a= " + $a) 
elseif ($a=2)
  Print( "$a= " + $a)
endif
 
Sleep( 2000 ) 
Goto(loop)

I'm using ARC 2017.05.14

Maybe the ElseIf issue has been fixed

Regards, Frank

PRO
USA
#5  

Frank:

Did you tried my code and is working for you ? I just tried my code with the new version and the bug is still present.

Simple example 2 (broken):


$a=11
print("Start")
if ($a=1)
  print("one")
elseif ($a=3)
  print("three")
elseif ($a=5)
  print("five")
endif
print("End")

output: script does not stop and keeps printing "Start".

Simple example 3 s[/s] WORKING:


$a=11
print("***Start")
if ($a=1)
  print("one")
ELSEif ($a=3)
  print("three")
ELSEif ($a=5)
  print("five")
ELSE
  print("another number")
endif
print("End")

output: Start ***Start another number End

#EDIT: This example works, Start is not duplicated, i confused the second start with ARC Start

PRO
USA
#7  

Frank:

didn't took me long to break your code:)


$a=3

:loop
print("before")
if ($a=1)
  Print( "$a= " + $a)
ELSEif ($a=2)
  Print( "$a= " + $a)
endif
print("after")
Sleep( 2000 )
Goto(loop)

The code above is broken

#8  

I can confirm what @ptp is saying... None of his examples seem to work...