Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I break, from outside of a loop in C?

I have a code:

void core()
{
   loop
   {
   first_process();
   secound_process();
   
   process_synchronizer();
   }
some_other_job();
}

In the process suynchronizer() I evaluate synchronization and if the criterion has not beeing satisfied it will break the loop. The problem is the break is not allowed in that function since it’s not in the loop.

void process_synchronizer()
{
   if(criterion)
      do something;
   else
      break;
}

But the break is not allowed there by the c compiler: break statement not within loop or switch

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

like this:

void core()
{
   bool exit_flag = false
   while (!exit_flag)
   {
       first_process(&exit_flag);
       if (!exit_flag)
           secound_process(&exit_flag);
       if (!exit_flag)
           process_synchronizer(&exit_flag);
   }
}

Or this:

void core()
{
   bool exit_flag = false
   while (!exit_flag)
   {
       exit_flag = first_process();
       if (!exit_flag)
           exit_flag = secound_process();
       if (!exit_flag)
           exit_flag = process_synchronizer();
   }
}

Then adjust your functions to return the appropriate value. Like this:

bool process_synchronizer()
{
   if(criterion)
      do something;
      return false;
   else
      return true;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading