Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

select case

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
loggercz

Bascom Member



Joined: 26 Mar 2017
Posts: 3

czechrepublic.gif
PostPosted: Sat Feb 12, 2022 9:54 am    Post subject: select case Reply with quote

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
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Sat Feb 12, 2022 10:37 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sat Feb 12, 2022 11:39 am    Post subject: Reply with quote

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 Shocked

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 165

blank.gif
PostPosted: Sat Feb 12, 2022 11:42 am    Post subject: Reply with quote

Petře
I'm sorry, but if I were you, I'd start here.
https://avrhelp.mcselec.com/
RS
Back to top
View user's profile
loggercz

Bascom Member



Joined: 26 Mar 2017
Posts: 3

czechrepublic.gif
PostPosted: Sat Feb 12, 2022 6:53 pm    Post subject: Reply with quote

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
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sat Feb 12, 2022 7:17 pm    Post subject: Reply with quote

I wrote
Quote:
select case -----end select allows to go directly to the line corresponding to the requested value

It is better to write
to go directly to the line corresponding to the requested ONLY !

your code is absolutly bad
Code:
A = 1
Select Case A
Case 1 : A = 3
Case 1 To 3 : A = 6
End Select

 

You can't write
case 1 and case 1 to 3

but you can write
Case 1
---
case 2 to 3
----

A select case/ end select
is only as the help said
Select Case I

Case 1 : Print "1"

Case 2 : Print "2"

Case 3 To 5 : Print "3-5"

Case Is >= 10 : Print ">= 10"

Case Else : Print "Not in Case statement"

End Select

jp Wink

_________________
pleasure to learn, to teach, to create
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 165

blank.gif
PostPosted: Sat Feb 12, 2022 7:21 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sat Feb 12, 2022 11:15 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sat Feb 12, 2022 11:43 pm    Post subject: Reply with quote

Duval JP wrote:
your code is absolutly bad

Really bad, you mean absolutely devilishly awfully irresponsibly schockingly bad? Very Happy
Didn't you notice that SZTRAD only wanted to demonstrate that within overlapping queries only the first match is executed?
Code:
A = 1
Select Case A
Case 1 : A = 3
Case 1 To 3 : A = 6
End Select

Quote:
You can't write
case 1 and case 1 to 3

Of course he can, the result will be:
A = 1 --> Case 1 --> A = 3
A = 2 --> Case 1 To 3 --> A = 6
A = 3 --> Case 1 To 3 --> A = 6
Back to top
View user's profile
loggercz

Bascom Member



Joined: 26 Mar 2017
Posts: 3

czechrepublic.gif
PostPosted: Sun Feb 13, 2022 11:47 am    Post subject: Reply with quote

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
View user's profile
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1161
Location: France

france.gif
PostPosted: Sun Feb 13, 2022 12:06 pm    Post subject: Reply with quote

saint MWS Pray 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
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 165

blank.gif
PostPosted: Sun Feb 13, 2022 4:13 pm    Post subject: Reply with quote

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.
Very Happy

RS
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 13, 2022 6:11 pm    Post subject: Reply with quote

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
View user's profile
hgrueneis

Bascom Member



Joined: 04 Apr 2009
Posts: 902
Location: A-4786 Brunnenthal

austria.gif
PostPosted: Sun Feb 13, 2022 7:46 pm    Post subject: Reply with quote

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
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 13, 2022 9:38 pm    Post subject: Reply with quote

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? Very Happy
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum