Errors, bugs, questions - page 2641

 
bool Func()
{
  for(int i=1;;++i)
  {
    if(i==1)
      continue;
    if(i==100)
      return false;
  }
}

void OnStart()
{
  Func();
}

There is such an advisor. It throws an error

'}' - not all control paths return a value      Test.mq5        10      1

If you comment out continue, it's fine. Am I the only one who thinks this is weird?

 
traveller00:

There is such an advisor. It throws an error

If you comment out continue, it's fine. Am I the only one who thinks it is strange?

If a function is not declared as void, it must return a value:

bool Func()
{
  for(int i=1;;++i)
  {
    if(i==1)
      continue;
    if(i==100)
      return false;
  }
  //---
  return(false);
}
 
The only exit from this function is through i==100. Why does he think that the appearance of continue results in an additional exit from the function?
 
traveller00:
The only way out of this function is through i==100. Why does he think that the appearance of continue leads to an additional exit from the function?

Single or multiple output is not important. The important thing is that YOU declared a function with a type other than void - which means that the function MUST return a value.

 

The function and returns a value

bool Func()
{
  for(int i=1;;++i)
  {
    if(i==1)
      continue;
    if(i==100)
      return false;
  }
}

This is the only way out of the function. That is, the only place where it has to return a value. And the C/C++ compiler agrees with me. If MQL still has its own standards on this, why does it build normally if continue is removed?

 
traveller00:

The function and returns a value

This is the only way out of the function. That is, the only place where it has to return a value. And the C/C++ compiler agrees with me. If MQL still has its own standards on this, why does it build normally if continue is removed?

The compiler doesn't know if the loop will be processed and if the code will reach return; Therefore, it requires a safety feature.

 
Alexey Viktorov:

The compiler does not know if the loop will be processed and if code execution will reach return; that's why it requires a safety precaution.

The C/C++ compiler knows this perfectly well. The MQL compiler is based on one of the C/C++ compilers, as I understand it. But then again, even if it has its own logic, why does it know without continue, but with continue this knowledge is gone?

 
traveller00:

The C/C++ compiler knows this perfectly well. The MQL compiler is based on one of the C/C++ compilers, as I understand it. But then again, even if it has its own logic, why does it know without continue, but with continue this knowledge is gone?

Probably because only a person at first glance looks at such code

bool Func()
{
  for(int i=1;;++i)
  {
    if(i==100)
      continue;
    if(i==100)
      return false;
  }
}
can figure out what is going to happen without thinking. In my opinion, it should be C and C++, not MQL. It is mql that protects against such accidental mistakes.
 
traveller00:

The function and returns a value

This is the only way out of the function. That is, the only place where it has to return a value. And the C/C++ compiler agrees with me. If MQL still has its own standards in this regard, why does it build normally if continue is removed?

This function of yours is a bug. If i==100, the next check for the same i==100 will not work. And you will get an infinite loop.

 
Dear developers, please point out the error.
Обсуждение статьи "Торговый эксперт с графическим интерфейсом: Наполнение функционалом (Часть II)"
Обсуждение статьи "Торговый эксперт с графическим интерфейсом: Наполнение функционалом (Часть II)"
  • 2018.06.10
  • www.mql5.com
Опубликована статья Торговый эксперт с графическим интерфейсом: Наполнение функционалом (Часть II): Автор: Anatoli Kazharski...
Reason: