View previous topic :: View next topic |
Author |
Message |
loggercz
Joined: 26 Mar 2017 Posts: 3
|
Posted: Sat Feb 12, 2022 9:54 am Post subject: select case |
|
|
Hi,
when I use :
SELECT CASE X
CASE 1: X=2
CASE 2: X=1
.
.
END SELECT
Which value have X? Is only one line executed?
Thanks
Petr
(BASCOM-AVR version : 2.0.8.5 ) |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Sat Feb 12, 2022 10:37 am Post subject: |
|
|
This is what the Select Case is for. If value match with one compared then rest of code (compares) are skipped. Means "Goto End Select" |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Sat Feb 12, 2022 11:39 am Post subject: |
|
|
Select case -----end select allows to go directly to the line corresponding to the requested value contrary to if-elseif__ elseif__ which tests all conditions
so in your example if X is 1 at the beginning it will take the value 2 before leaving the loop.
Can you specify your request?
JP _________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
|
Back to top |
|
|
loggercz
Joined: 26 Mar 2017 Posts: 3
|
Posted: Sat Feb 12, 2022 6:53 pm Post subject: |
|
|
Because the program behaves strangely, I wondered if there was a problem
when I change the tested value inside the SELECT CASE command processing.
But only the first valid condition seems to be executed, even if another valid one follows.
A = 1
Select Case A
Case 1 : A = 3
Case 1 To 3 : A = 6
End Select
Print A
3
Thanks for the posts |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Sat Feb 12, 2022 7:21 pm Post subject: |
|
|
It is the same for all languages.You look for the first match and execute the conditions behind it. There's no reason for it to work any other way.
RS |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Feb 12, 2022 11:15 pm Post subject: |
|
|
Duval JP wrote: | Select case -----end select allows to go directly to the line corresponding to the requested value contrary to if-elseif__ elseif__ which tests all conditions
...
It is better to write
to go directly to the line corresponding to the requested ONLY ! |
Excuse, but both is utter nonsense.
How would the µC magically know for select case it's the third line? There's no indexed jump table or such.
To check if there's a matching query, comparisons have to be done for each query line, but only till there is a match.
This is exactly the same in select case as in if/elsif.
After a match is found, the query block is left immediately, if no match occurs, it runs the whole block.
If select case vs. if/elseif is to compare, the first gives a cleaner code structure.
That's the true difference.
In the simulator you can check the run-time for different values of Itm.
The compiler assembles the code for both versions slightly different, that's 4 cycles difference - which is nothing at all.
For preset: Itm = 1 both versions take 91 cycles.
Code: | $Regfile="m328def.dat"
$Crystal=4000000
$hwstack=40
$swstack=16
$framesize=32
Dim Itm As Byte
Itm = 3
' takes 101 cycles
Select Case Itm
Case 1: Print 1
Case 2: Print 2
Case 3: Print 3
Case 4: Print 4
Case Else: Print "Else"
End Select
' takes 105 cycles
If Itm = 1 Then
Print 1
ElseIf Itm = 2 Then
Print 2
ElseIf Itm = 3 Then
Print 3
ElseIf Itm = 4 Then
Print 4
Else
Print "Else"
End If
End |
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
|
Back to top |
|
|
loggercz
Joined: 26 Mar 2017 Posts: 3
|
Posted: Sun Feb 13, 2022 11:47 am Post subject: |
|
|
thanks to all, basically, I asked inaccurately if only the first match will really be compared and the others will skip it. So is it... |
|
Back to top |
|
|
Duval JP
Joined: 22 Jun 2004 Posts: 1161 Location: France
|
Posted: Sun Feb 13, 2022 12:06 pm Post subject: |
|
|
saint MWS I like you, you are a god to me, every time I make a logical comment you give me my punishment I said bad I should have said wrong, My English is quite primitive I am sorry.
I give bascom classes in a fablab, where I try to give basic notions and logic and I think my students appreciate my approach
Yes indeed select case comes out when they meet the first game
but my logic tells me not to do fuzzy logic and for me
Case 1 : A = 3
Case 1 To 3 : A = 6 ' this line is not logic after the upper one
Translated with www.DeepL.com _________________ pleasure to learn, to teach, to create |
|
Back to top |
|
|
SZTRAD
Joined: 30 Dec 2019 Posts: 165
|
Posted: Sun Feb 13, 2022 4:13 pm Post subject: |
|
|
And since Bascom is based on qbasic if my memory serves me right, we have no choice but to look at what our elders taught us in school.
"Select Case executes specific code depending on the value of an expression. The expression is evaluated once, and compared against each Case, in order, until a matching expression is found. The code inside the matching Case branch is executed, and the program skips down to the end of the Select Case block. Case Else matches any case not already matched, so if there is a Case Else, at least one Case is guaranteed to be executed. If no Cases match, the whole Select Case block will be skipped."
Where I am now, I have limited access to the book database. I'm sorry it took a little while to get it to me so I could copy it exactly from the '89 original.
RS |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Feb 13, 2022 6:11 pm Post subject: |
|
|
Duval JP wrote: | you give me my punishment I said bad I should have said wrong |
Wrong, I did correct you. And as you aggravated with 'absolutely', you'd have to say 'absolutely wrong'.
The sample was done to test behavior of a query where expression-ranges overlap.
Result was that only the first match executes.
To be able to show this, the expressions had to be this way.
Which took you to stampede-mode. |
|
Back to top |
|
|
hgrueneis
Joined: 04 Apr 2009 Posts: 902 Location: A-4786 Brunnenthal
|
Posted: Sun Feb 13, 2022 7:46 pm Post subject: |
|
|
There is really no need for any blame or discussion, just take a look at the HELP.!!! This is overblown!
Regards
Hubert |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sun Feb 13, 2022 9:38 pm Post subject: |
|
|
hgrueneis wrote: | There is really no need for ... discussion, ... |
Well, some of the discussed points were worth it for me.
Have I to ask you next time? |
|
Back to top |
|
|
|