Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

switch default

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, This is from K&B mock exam. Please let me know why the output is
2
def
2.

I thought it would be
def
2
def
2.

The reason for my assumption is if there is no match before default, default and any statement after the default will be executed. Please advice
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the 'case' if z = 0?
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first time through the loop z is 0 so "2 " is printed because x-2 equals 0. The second time through the loop z is 1 so "def " is printed and then it prints "2 " because there is no break at the bottom of your default case. Then the loop ends.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran, I was thinking if case matches before default, case will be executed. If case is not matched, all the statements from default will be executed. But here, case that is matching after default is executed. Please let me know the logic/rule behind that.

Abimaran Kugathasan wrote:Which is the 'case' if z = 0?

Thanks Tom. I got your point and trying to understand the rule behind it.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It first check whether any 'case's match and, if any thing don't match, then only, the 'case' will be default. You clear it with the K&B's book!
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matching the case after default is not there. Based on your comments, what I understand is, First it will try to match all cases before and after default. If it cannot find match, then default will be executed. Please let me know if my understanding is correct

Abimaran Kugathasan wrote:It first check whether any 'case's match and, if any thing don't match, then only, the 'case' will be default. You clear it with the K&B's book!

 
Greenhorn
Posts: 6
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Matching the case after default is not there. Based on your comments, what I understand is, First it will try to match all cases before and after default. If it cannot find match, then default will be executed. Please let me know if my understanding is correct

Abimaran Kugathasan wrote:It first check whether any 'case's match and, if any thing don't match, then only, the 'case' will be default. You clear it with the K&B's book!


you nailed it!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harikrishna Gorrepati wrote:Matching the case after default is not there. Based on your comments, what I understand is, First it will try to match all cases before and after default. If it cannot find match, then default will be executed. Please let me know if my understanding is correct


That's correct. And check if there is a break......., I think, you know the rest!
 
reply
    Bookmark Topic Watch Topic
  • New Topic