how to jump out of while()?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-03-04 9:35 AM
hey there
I have the code below
uint32_t startTime = HAL_GetTick();
while (HAL_GetTick() - startTime < 5000)
{
for (int i = 0; i < 3; i++)
{
if (strstr(InputMSG, answerSearch[i]))
{
resault = 1;
break;
}
}
}
where I want to check if there are any match of answerSearch[i] within InputMSG array and if there is, jump out of while, but what I see is that the process lasts as long as 5000ms, even though the match is found. the thing is that even one match is enough and as soon as one match is found, I want it to jump out of while().
what can I do to make it jump as soon as it finds the first match?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-03-04 10:34 AM
uint32_t startTime = HAL_GetTick();
while (HAL_GetTick() - startTime < 5000)
{
int i;
for(i = 0; i < 3; i++)
{
if (strstr(InputMSG, answerSearch[i]))
{
resault = 1;
break;
}
}
if (i != 3) break; // loop exited early
}
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-03-04 9:48 AM
Simply dont use for or add next if resault==1 break after for or in while add && resault==0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2021-03-04 10:34 AM
uint32_t startTime = HAL_GetTick();
while (HAL_GetTick() - startTime < 5000)
{
int i;
for(i = 0; i < 3; i++)
{
if (strstr(InputMSG, answerSearch[i]))
{
resault = 1;
break;
}
}
if (i != 3) break; // loop exited early
}
Up vote any posts that you find helpful, it shows what's working..
