About Question enthuware.ocajp.i.v7.2.1239 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1239 :

Post by AndaRO »

I don't understand why case 1 : is not executed.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1239 :

Post by admin »

In the first iteration of the for loop, when i is 0, the switch increments i to 1. When the switch ends, the increment section of the for loop executes and increments i, which makes it 2. So the switch is never executed with a value of 1.
If you like our products and services, please help us by posting your review here.

cccdan
Posts: 1
Joined: Mon Dec 04, 2017 5:09 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1239 :

Post by cccdan »

muttley wrote:I didn't understand why in the first loop the B is not printed:

switch(i++){ //after this line the var i will be 1
Not really. switch doesn't behave in the standard way, as it happens with, say, if (i++)

The thing many people don't understand is that the "case" part from within the switch block is treated as if it's part of the expression inside the switch parantheses. So i will still be zero(or whatever value it has) when it comes to case 0: case 1: and so on... the post increment only gets executed after execution gets past the semicolon of case (if there is a match) or when the method is exited.

it's something like this:

Code: Select all

int i = 0;

switch (i++) { // i is 0
case 0 //i still 0: //now i is 1
}

bomicbon
Posts: 5
Joined: Sun Jan 21, 2018 7:33 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1239 :

Post by bomicbon »

OK this is why it's not B and then BREAK LOOP:
i = 0;
switch(i++); // 0 is accepted by the switch statement, then incremented immediately afterwards to i = 1
// so none of the cases are accepted in first loop
// proceeds immediately to i=2
// the rest is explained pretty well by enthuware

Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests