Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Understanding DEFAULT case in SWITCH statements

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the given code



The output is
3
4

This surprises me.
I would have expected
default
3
4

How do I interpret this?
Given that case statements have to be compile constants then what actually happens when a class is compiled?
It must mean that a map or similar construct is created of the case statements and runtime merely outputs the contents of that map.

Thoughts............?
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jon Greenwood wrote:
The output is
3
4

This surprises me.
I would have expected
default
3
4



Why should it go to the default case? It is a match for the 3 case, so shouldn't it go there?

Henry
 
Jon Greenwood
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My assumption was that

(similar to if statements)

the default would evaluate to true and become the execution point

Clearly that cannot be the case

so I assume that
all cases must be evaluated
and then the default (regardless of the order of the statements)

Question is ..is that execution point defined at Compile or at runtime?

It would appear to be defined at Compilation

 
Sheriff
Posts: 7647
522
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. default case gets executed if no exact match found.
2. In case exact match found and it does not contain break statement, then case/-s below get executed (including default if it is below initially executed case) until break statement is found and executed or reaches the end of switch statement.
3. In case of no exact match found, default case gets executed regardless its position among other statements, and then again, if no break statement within default case and there are some cases below it, they get executed until break statement found and executed, or no more cases left below.
 
Henry Wong
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jon Greenwood wrote:
the default would evaluate to true and become the execution point

Clearly that cannot be the case



Oh I see. Confusion makes sense.  The default case is the target only if all other case statements fails -- this is true regardless of its location.

Jon Greenwood wrote:
Question is ..is that execution point defined at Compile or at runtime?

It would appear to be defined at Compilation



There are both compile time and run time components. The branch tables, and the execution code are done at compile time, but of course, there is a runtime component, as the variable in the switch statement isn't available at compile time.

Henry
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic