Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top