473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

About evaluation in loops

Hi.
This is not a critical question.
Consider this snippet:

switch ((myenum) hashtable [key])
{
case myenum.first :
...
}

I just want to know whether the expression
(myenum) hashtable [key]
needs to be evaluated for each case declared in the switch
statement.
The alternative would be as i have done yet to make sure:
myenum var = (myenum) hashtable [key];
switch(var)
....

Thank you

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 949
Dennis Myren wrote:
switch ((myenum) hashtable [key])
{
case myenum.first :
..
}

I just want to know whether the expression
(myenum) hashtable [key]
needs to be evaluated for each case declared in the switch
statement.


I would be shocked to find out that the compiler wasn't smart enough to
optimize this on its own... most compilers turn switch statements into
jump tables, sometimes with binary trees used to short-circuit large
parts of the table. This really wouldn't be possible unless the
expression was only evaluated one time and the result stored in a
temporary location...
Nov 16 '05 #2
All,

Simply take a look at the IL generated and you will know *exactly*
what happens. The beauty of ILDASM...

John
Dennis Myren <de**********@hotmail.com> wrote in message news:<Ol**************@tk2msftngp13.phx.gbl>...
Hi.
This is not a critical question.
Consider this snippet:

switch ((myenum) hashtable [key])
{
case myenum.first :
..
}

I just want to know whether the expression
(myenum) hashtable [key]
needs to be evaluated for each case declared in the switch
statement.
The alternative would be as i have done yet to make sure:
myenum var = (myenum) hashtable [key];
switch(var)
...

Thank you

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #3
> switch ((myenum) hashtable [key])
{
case myenum.first :
..
}

I just want to know whether the expression
(myenum) hashtable [key]
needs to be evaluated for each case declared in the switch
statement.
The alternative would be as i have done yet to make sure:
myenum var = (myenum) hashtable [key];
switch(var)

It is guaranteed by the language that the expression in the switch is
evaluated exactly one time.

But what does this have to do with loops as you stated in the headline?

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ilias Lazaridis | last post by:
" A cooperation between Sun Microsystems and IBM&Co. in conjunction with liberal & high evolutive communities would result in an nearly unbeatable programming platform. My evaluation has shown:...
5
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
6
by: Derek | last post by:
I came upon the idea of writting a logging class that uses a Python-ish syntax that's easy on the eyes (IMO): int x = 1; double y = 2.5; std::string z = "result"; debug = "Results:", x, y,...
25
by: mdh | last post by:
Given the expression: while (isaspace(c = *s++)) x+1; (s is an array) Does the increment to 's' occur after "x+1" is evaluated, or after the content of s is assigned to c? Is there a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.