Working through some exercise, and this loop behaviour is not as I expected:
void primeFunc(int &number){
bool prime_check = false;
for( int control = number; control > 1; --control){
printf("\tcontrol = %d\n", control);
for( int sub_control = control-1; sub_control > 1; --sub_control){
printf("\t\tsub_control = %d\n", sub_control);
if(control % sub_control == 0){
prime_check = false;
break;
}
else prime_check = true;
// printf("\nPrime: div/quo :: %d/%d", control, sub_control);
}
if(prime_check == true){
printf("\n%d", control);
}
}
}
// function call
int num = 5;
primeFunc(num); //<- clearly showing how parameter is passed to func argument ->
The output of the snippet above:
5 control = 4
sub_control = 3
sub_control = 2
control = 3
sub_control = 2
3 control = 2
2
How is 2 evaluating successfully here.
- I exaplained it as control = 2; the outer for loop starts (control decrements to 1);
- print(control=2) runs;
- sub_control evaluates to 1; inner for loop exits
In this case, the outer for loop isnt even entered print(control=2) doesnt execute.
edit:
The question really is understanding how the number 2 evaluates with prime_check = true
.
- the for loop is not entered at all, looking at the output