Page 1 of 1

[HD Pg 0, Sec. 7.6.2 - terminating-an-iteration-of-a-loop-using-continue]

Posted: Fri Oct 05, 2018 12:40 pm
by flex567
In this section there is a sentence that is not completely clear
In case of a for loop, and the control executes the updation section before moving on to the next iteration.
Can you explain what was meant with this sentence?

Re: [HD Pg 0, Sec. 7.6.2 - terminating-an-iteration-of-a-loop-using-continue]

Posted: Fri Oct 05, 2018 8:44 pm
by admin
It makes sense when read in continuation of the previous sentence:
In other words, when a loop encounters the continue statement, the rest of the statements in the loop are skipped and the control moves on to execute the next iteration (depending on the loop condition). In case of a for loop, and the control executes the updation section before moving on to the next iteration.
The word "and" is a typo. Should be removed.
So, if you have something like:

Code: Select all

for(int i=0; i<5; i++){
  if(i==2) continue; 
  System.out.println(i); 
}
When i is 2, continue statement will be executed. The print statement will be skipped. But before next iteration is started, the updation section of the for loop i.e. i++ will be executed. So, in the next iteration i will be 3.

Re: [HD Pg 0, Sec. 7.6.2 - terminating-an-iteration-of-a-loop-using-continue]

Posted: Sat Oct 06, 2018 8:09 am
by flex567
In the case above after the continue execution goes to 'i++' and after that it goes to checking condition?

Re: [HD Pg 0, Sec. 7.6.2 - terminating-an-iteration-of-a-loop-using-continue]

Posted: Sat Oct 06, 2018 8:24 am
by admin
yes.
1. updation (in case of for),
2. condition check.
3. if condition is true, loop statements.

Re: [HD Pg 0, Sec. 7.6.2 - terminating-an-iteration-of-a-loop-using-continue]

Posted: Sat Oct 06, 2018 8:39 am
by flex567
Aha, thank you for clarification