I need to know what does this for loop syntax actually says.
for(int parity : {0, 1}) {
int low = 1, high = n;
if(low % 2 != parity) low++;
if(high % 2 != parity) high--;
}
I need to know what does this for loop syntax actually says.
for(int parity : {0, 1}) {
int low = 1, high = n;
if(low % 2 != parity) low++;
if(high % 2 != parity) high--;
}
Alright lets break this line by line.
for (int parity : {0,1}) {
results in:
for (int parity : std::initializer_list<int>{0, 1}) {
initializer_list<int>{0,1}
has two elements so the for loop will loop two times
low is assigned 1, high is assigned n
which I assume is the high end?
if(low % 2 != parity)
will result in 1
on the first iteration so the value of low will become 2
;
likewise high will decrease/increase its value depending upon the value of n
at the end of loop low
will be equal to 3
, the value of high will depend upon the value of n
which is unknown in this context.