Replace if/else cascade with goto

Hi,

I’m using a State Machine task to handle the flow of my program. When a state is selected (other than STAND BY), the State Machine wake up the associated task with the “xTaskNotifyGive” function as all other tasks are locked by “ulTaskNotifyTake( pdTRUE, portMAX_DELAY);”

During the execution of those state-related tasks, it can happen that there is a problem and that I have to stop them. In order to do that, a Safety task set a flag “ContinueTask”. In the state-related task, we regularly check this flag. If it is false, the code following won’t be executed.

For the moment, the structure of the code look like this:

ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally){
    //some code
}
ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally){
    //some code
}
...
ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally){
    //some code
}

The problem is that if the flag is set up the first time we check it, it will still check it for the next part of the code.

On way to solve this is to use a cascade of if/else statements like this:

ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally){
    //code
}
else{
    ContinueTaskInternally = ContinueTaskCopy();
    if (ContinueTaskInternally){
        //code
    }
    else{
        ContinueTaskInternally = ContinueTaskCopy();
        if (ContinueTaskInternally){
            //code
        }
        else{
            ....
        }
    }
}

But if we check this flag a lot of time if the task, the number of indentation will be really high and it won’t be readable.

I was wondering if, in this case, it’s possible to use the “goto” statement, like this:

ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally) goto exitTask;
//some code

ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally) goto exitTask;
//some code
...

ContinueTaskInternally = ContinueTaskCopy();
if (ContinueTaskInternally) goto exitTask;
//some code

exitTask:
//code before exiting task

What do you think about it ?

Yes, it is possible as it is valid C. However this does not appear to be a FreeRTOS question. To maintain the value of this forum specifically for FreeRTOS users our request is that it be dedicated to FreeRTOS related questions only, and not general embedded or programming questions. Thank you for your understanding.