I like the while
variant more, because it expresses that the number of iterations is variable. Additionally, it is better to use the boolean and &&
instead of the bitwise and &
here.
best coding practices, loops [closed]
-
20-07-2023 - |
Pregunta
I'm writing some code and I have two possible methods of achieving the goal, so I thought I would ask on here and find out what people think, so the first method is
valid=0;
count=0;
while(!valid&&count<6)
{
valid=read_fix_from_flash(download_fix_starting_position.ul);//returns 1 if successful
count++
}
or the alternative
valid=0;
for(count=6;count>0;count--)
{
valid=read_fix_from_flash(download_fix_starting_position.ul);//returns 1 if successful
if(valid){
break;
}
}
In my opinion the second method is the better option but I have seen examples using the first method.
Solución
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow