wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Confused by increments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused by increments" Watch "Confused by increments" New topic
Author

Confused by increments

Fitz Herbert
Greenhorn

Joined: Mar 22, 2003
Posts: 21
Having a few problems with a question involving increments within loop/switch statements.
The question code is as follows
which prints 356
WHY?
I realized that the case statement '0'is not caught
my reasoning a bit iffy
then i looked at the increments and started to lose
track
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Originally posted by Fitz Herbert:
Having a few problems with a question involving increments within loop/switch statements.
The question code is as follows
which prints 356
WHY?
I realized that the case statement '0'is not caught
my reasoning a bit iffy
then i looked at the increments and started to lose
track

i is int, so i fail the case of "0", in switch statement, is says "i++", so in next loop, i become 2+1=3. so it skip i=2 case.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

I really hate problems like this. Asking what happens when intentionally confusing code is written loses the point that you should always strive for clarity.
Anyway, here's what is happening:
The first time the switch statement is hit, the value of i is 0. That doesn't match any of the case statments. It doesn't match '0', because that is a character, and when view as an integer, its value is 48, the ASCII value of the character 0. Therefore nothing is printed.
i is incremented twice, once by the incrementer in the switch statement, then by the for loop incrementer. The next switch uses a value of i = 2. That matches a case, so "3" is printed out. (Did I mention I hate this code.)
i is incremented twice more, so it has a value of 4. That matches a case so "5" is printed out. There is no break, so execution falls through to the case 'E' as well and prints out "6". The value of i after the switch is 5, so the loop terminates and we're done.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I really hate problems like this.

Exactly, why would anyone write code in which the 'case' for 1 prints 2?


Please ignore post, I have no idea what I am talking about.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by William Barnes:

Exactly, why would anyone write code in which the 'case' for 1 prints 2?

To be obnoxious? Or maybe they are trying to write test questions for the SCJP exam...
[ May 19, 2003: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
that is what i figured..
Fitz Herbert
Greenhorn

Joined: Mar 22, 2003
Posts: 21
Hello
SCJ2 questions
thankyou for the response
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2833
    
  11

Originally posted by Layne Lund:

To be obnoxious? Or maybe they are trying to write test questions for the SCJP exam...


That doesn't sound like an "either-or" situation to me!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Greg Charles:

That doesn't sound like an "either-or" situation to me!

*ROFL* Thanks for cheering me up for the day.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused by increments
 
Similar Threads
Flow control!
Dans Questions : logic
Do while error, I'm going crazy
allowable values for case labels ?
I thought I understood this stuff.....