Loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Loop

In for(i=4; i<8; i++) { if (i == 6) { continue; } sum += i; } does it execute i++ first or the code block first?

19th Mar 2018, 6:16 PM
Riley Myers
Riley Myers - avatar
6 Answers
+ 5
Yes. Step by step, your example will do: i set to 4 i is less than 8 so do the code block, then increment i ( so i is now 5 ) i is less than 8 so do the code block, then increment i ( so i is now 6 ) i is less than 8 so do the code block, then increment i ( so i is now 7 ) i is less than 8 so do the code block, then increment i ( so i is now 8 ) i is not less than 8, so don't do the code block and jump after... but in the code block there's first a conditional "continue" statement: so when i is equal to 6, the last part of the code block is not executed (sum += i) ;)
22nd Mar 2018, 12:13 AM
visph
visph - avatar
+ 3
The code block first
19th Mar 2018, 6:45 PM
Deddy Tandean
+ 2
first i is set to 4 and code block executes then i value increments till the condition i<8 once the condition fails execution of for loop is completed
19th Mar 2018, 6:53 PM
kaliki chandu
kaliki chandu - avatar
+ 2
The for parenthesis parameters is semi-colon separated in three parts: for (<initialisation>; <condition>; <end loop code>)... <initialisation> is done once, before starting the loop, and never repeated, and usualy is used to initialize a counter. <condition> is tested at each iteration, before starting code block execution, and exit/jump after the loop code block if false. <end loop code> is done at each iteration end (after code block execution), and is usualy used to increment or decrement the counter.
20th Mar 2018, 1:16 AM
visph
visph - avatar
+ 1
Thx
22nd Mar 2018, 1:26 AM
Riley Myers
Riley Myers - avatar
0
So it will set i to 4, check to see if it's less than 8, do the code block, increment i by 1, and then do the whole thing again except for the part i = 4?
21st Mar 2018, 9:54 PM
Riley Myers
Riley Myers - avatar