About Question enthuware.ocajp.i.v7.2.1239 :
Moderator: admin
-
- Posts: 31
- Joined: Wed Feb 08, 2017 5:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1239 :
I don't understand why case 1 : is not executed.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1239 :
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.
-
- Posts: 1
- Joined: Mon Dec 04, 2017 5:09 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1239 :
Not really. switch doesn't behave in the standard way, as it happens with, say, if (i++)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
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
}
-
- Posts: 5
- Joined: Sun Jan 21, 2018 7:33 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1239 :
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
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
Who is online
Users browsing this forum: Bing [Bot] and 54 guests