• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Assertions

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code below, what happens when assertions are disabled?The answer given is that it prints ABCE.

My question is if it prints E then why doesn't it go through next iterations of the for loop and print more letters?please explain.


class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
return "E";
}
public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.print(c.m1(i));
}}}
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only reason it will only return E when none of the cases match. This happens when i is 3 in this case.
[ July 18, 2006: Message edited by: Keith Lynn ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The for loop runs from 0-3. Thus, you will only get 4 characters.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When 3 is passed into the switch statement should it not go into the default case and throw an assertion error and cause the program to fail?

I can see that it is actually going into the default case but why isn't
throwing an assertion error and causing the program to fail?

Thanks
 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In the code below, what happens when assertions are disabled?The answer given is that it prints ABCE.



We are discussing the case when assertions are disabled. If assertions are enabled, then the default case will be entered and an assertion error will be thrown.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Saumya,

You asked why the code doesn't go through the next iterations of the for loop. It actually goes through all the iterations.

Each time the for loop calls c.m1, only the String which belongs to the case-statement with the corresponding value of i is returned. Return means that the code immediately goes back to the calling method. So, when i =0 (in the for loop), only the value of case 0 is returned and printed. The same goes for 1 and 2. Because i = 3 is matched to the default case, and assert is off (which means that that part of the code is ignored), the method m1 is allowed to finish and therefore returns "E".

The following code "proves" that all four iterations run:

class C {
String m1(int i) {
switch (i) {
case 0: return "A";
case 1: return "B";
case 2: return "C";
default: assert false;
}
return "E";
}

public static void main(String[] args) {
C c = new C();
for (int i = 0; i < 4; i++) {
System.out.println(c.m1(i));
System.out.println("Iteration number: " + (i + 1) );
}
}
}

I hope this clarifies your question.

Regards,

Roger
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic