This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Case/while/break question

Hi,

I'm using the uVision Realview compiler and am confused about the 'break' statement inside the 'while' loop inside the 'case' statement : does it exit the while loop and executes the 'extra code' or does it exit also the 'case' statement completely without executing the 'extra code'?

use next (abstract) code:


switch ( x ) {

  case y:

    while(1){

      break;

    }

  'extra code'

  break;

}

Thanks

Henk

Parents
  • The break exits the while loop only.

    If you want to skip the 'extra code' in addition to exiting the while loop, you can use and test a boolean variable after the while loop that indicates whether the while loop was exited regularly (if it's not an infinite loop like in the example) or by hitting a break statement.

    As long as you don't believe that there's no good use whatsoever for a goto statement, breaking out of deeply nested control structures is one of the few places where a goto would be useful.

Reply
  • The break exits the while loop only.

    If you want to skip the 'extra code' in addition to exiting the while loop, you can use and test a boolean variable after the while loop that indicates whether the while loop was exited regularly (if it's not an infinite loop like in the example) or by hitting a break statement.

    As long as you don't believe that there's no good use whatsoever for a goto statement, breaking out of deeply nested control structures is one of the few places where a goto would be useful.

Children