View previous topic :: View next topic |
Author |
Message |
AndersL
Joined: 25 Jan 2010 Posts: 92 Location: Kragerø,Norway
|
Posted: Tue Jan 15, 2019 2:04 pm Post subject: Odd behavior with select-case |
|
|
Hi,
I came accros this odd behavior with select-case statement used together with Goto.
Two statements separated by a ":" on the Case Else line will throw an error if last statement is a Goto.
If the second statement are moved one line down, the code will compile and work as expected.
Its no big deal doing this as an workaround, but I'm curious why.
Compiler version :2.0.8.1
Compiler build :2.0.8.1.000
IDE version :2.0.8.1.001
Windows OS :Windows 10 Pro
Code: | $regfile = "m328pdef.dat"
$crystal = 16000000
$hwstack = 100
$swstack = 100
$framesize = 100
$baud = 38400
$sim
Dim P As Byte
' THIS GIVES > Error : 323 Line : 19 Label Too Long [ "UNKNOWN ERROR" : Goto Brake_here]
P = 1
Select Case P
Case 1 : Print "All is ok"
Case 2 : Print "Some error occurred" : Goto Brake_here
Case Else : Print "Unknown error" : Goto Brake_here
End Select
' THIS WILL COMPILE AND WORK AS EXPECTED
P = 2
Select Case P
Case 1 : Print "All is ok"
Case 2 : Print "Some error occurred" : Goto Brake_here
Case Else : Print "Unknown error"
Goto Brake_here ' THE GOTO FOR CASE ELSE MOVED TO NEXT LINE
End Select
Brake_here:
Print "P= " ; P
End |
(BASCOM-AVR version : 2.0.8.1 ) _________________ Anders |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Wed Jan 16, 2019 3:44 am Post subject: |
|
|
I have always done my select/case like this so never had that problem
I find it easier to read
Regards Paul
Code: |
Select Case P
Case 1
Print "All is ok"
Case 2
Print "Some error occurred"
Goto Brake_here
Case Else
Print "Unknown error"
Goto Brake_here
End Select
|
|
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 584 Location: Cleveland, OH
|
Posted: Thu Jan 17, 2019 8:49 pm Post subject: |
|
|
OK, so I'll ask the question?
Why do you even have the Goto statement?
The Case Select should run the code associated with the Case, or the Else catch-all, and then exit.
You don't have to re-direct the exit yourself.
JC |
|
Back to top |
|
|
AndersL
Joined: 25 Jan 2010 Posts: 92 Location: Kragerø,Norway
|
Posted: Fri Jan 18, 2019 12:38 pm Post subject: |
|
|
Hi,
The code is an extract/simplified version of a function I wrote, witch should return with P if any of several Select-Case statements
fails.
I also usally write statements on next line as Paulvk sugest, but for some reason I opted to use : and one line this time.
If you look at Help example for Select-Case, its legal to have case statements on same line separated with :.
Edit:I'll send this to support(as I should have in the first place) _________________ Anders |
|
Back to top |
|
|
Micha
Joined: 03 Oct 2006 Posts: 56
|
Posted: Wed Mar 06, 2019 1:33 pm Post subject: |
|
|
Have a look to the following Statements:
Redo and Continue
But this works only in Loops.
[/list] |
|
Back to top |
|
|
|