Blogger Template by Blogcrowds

Friday, April 10, 2009

The break Statement

The break statement has two uses. You can use it to terminate a case in the switch statement. You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test.

When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. For example,

#include <stdio.h>int main(void)
{
  int t;

  for(t=0; t<100; t++) {
    printf("%d ", t);
    if(t==10) break;
   }

   return 0;
}

prints the numbers 0 through 10 on the screen. Then the loop terminates because break causes immediate exit from the loop, overriding the conditional test t<100.

Programmers often use the break statement in loops in which a special condition can cause immediate termination. For example, here a keypress can stop the execution of the look_up() function:

void look_up(char *name)
{
  do {
    /* look up names ... */
    if(kbhit()) break;
  } while(!found);
  /* process match */
}

The kbhit() function returns 0 if you do not press a key. Otherwise, it returns a nonzero value. Because of the wide differences between computing environments, neither Standard C nor Standard C++ defines kbhit(), but you will almost certainly have it (or one with a slightly different name) supplied with your compiler.

A break causes an exit from only the innermost loop. For example,

for(t=0; t<100; ++t) {
  count = 1;
  for(;;) {
    printf("%d ", count);
    count++;
    if(count==10) break;
  }
}

prints the numbers 1 through 10 on the screen 100 times. Each time execution encounters break, control is passed back to the outer for loop.

A break used in a switch statement will affect only that switch. It does not affect any loop the switch happens to be in.

The goto Statement

Since C/C++ has a rich set of control structures and allows additional control using break and continue, there is little need for goto. Most programmers’ chief concern about the goto is its tendency to render programs unreadable. Nevertheless, although
the goto statement fell out of favor some years ago, it occasionally has its uses. There are no programming situations that require goto. Rather, it is a convenience, which, if used wisely, can be a benefit in a narrow set of programming situations, such as jumping out of a set of deeply nested loops.

The goto statement requires a label for operation. (A label is a valid identifier followed by a colon.) Furthermore, the label must be in the same function as the goto that uses it — you cannot jump between functions. The general form of the goto statement is

goto label;
.
.
.
label:

where label is any valid label either before or after goto. For example, you could create a loop from 1 to 100 using the goto and a label, as shown here:

x = 1;
loop1:
  x++;
  if(x<100) goto loop1;

The return Statement

The return statement is used to return from a function. It is categorized as a jump statement because it causes execution to return (jump back) to the point at which the call to the function was made. A return may or may not have a value associated with it. If return has a value associated with it, that value becomes the return value of the function. In C, a non-void function does not technically have to return a value. If no return value is specified, a garbage value is returned. However, in C++, a non-void function must return a value. That is, in C++, if a function is specified as returning a value, any return statement within it must have a value associated with it. (Even in C, if a function is declared as returning a value, it is good practice to actually return one.)

The general form of the return statement is

return expression;

The expression is present only if the function is declared as returning a value. In this case, the value of expression will become the return value of the function.

You can use as many return statements as you like within a function. However, the function will stop executing as soon as it encounters the first return. The } that ends a function also causes the function to return. It is the same as a return without any specified value. If this occurs within a non-void function, then the return value of the function is undefined.

A function declared as void may not contain a return statement that specifies a value. Since a void function has no return value, it makes sense that no return statement within a void function can return a value.

The return statement will be discussed more detail later.

Older Posts