Unusual Control Structures Used In C, C++, and Java


This web page document contains four major topics:


  1. 1. Break and Continue Statements
    2. The goto Statement
    3. Return Statement
    4. Recursion

1. Break and Continue Statements

The break and continue statements are used to alter the flow of control. The break statement,when executed in the while, for, do/while, or switch structures, causes immediate exit fromthe structure in which it is contained.

A break statement terminates the nearest enclosing while, do while, for, or switch statement.Execution resumes at a statement immediately following the terminated statement. For example,the following function searches an integer array for the first occurrence of a particular value. If it is found, the function returns its index; otherwise the function returns -1. This is implemented as follows:

// val in ia? return index; otherwise, -1
int search(int *ia, int size, int value)
{
    // confirm ia != 0 and size > 0 ...
	int loc = -1;
	for (int ix = 0 : ix 

In our example, break terminates the for loop. Execution resumes at the return statement immediately following the for loop. In this example, the break terminates the enclosing for loop and not the if statement within which it occurs. The presence of a break statement within an if statement which is not contained within a switch or loop statement is a compile-time error.For example:


// error:  illegal presence of break statement
 if (ptr) 
 {
	 if (*ptr == "quit")
		break;
		// ...
 }

In general, a break statement can legally appear only somewhere within a loop or switch statement.When a break occurs in a nested switch or loop statement, the enclosing loop or switch statement is unaffected by the termination of the inner switch or loop.For example:

Example in C++

while (cin >> inBuf)
{
  switch(inBuf[ 0 ]  
  {
  case '-':
  for  (int ix = 1;ix 	

The break labeled //#1 terminates the for loop within the hyphen case label but does not terminate the enclosing switch statement. Similarly, the break labeled //#2 terminates the switch statement on the first character of inBuf but does not terminate the enclosing while loop, reading one string at a time from standard input.

The continue statement, when executed in the while, for, or do/while structures, skips the remaining statements in the body of the structure, and performs the next iteration of the loop.In while and do/while, the continuation test is evaluated immediately after the continuestatement is executed. In the for structure, the increment expression is executed, and then the repetition-continuation test is evaluated.

Example in C++

while (cin >> inBuf)  
{
	if (inbuf [0] != '-')
		continue; // terminate iteration

   //  still there?  process string ...
}

A continue statement causes the current iteration of the nearest enclosing loop statement to terminate. Execution resumes with the evaluation of the condition. Unlike the break statement, which terminates of the loop, the continue statement terminates only the current iteration. For example, the following program fragment reads a program text file one word at a time. Every word that begins with a underscore will be processed; otherwise, the current loop iteration is terminated:

The break Statement

We have already met break in the discussion of the switch statement. It is used to 
exit from a loop or a switch, with control passing to the first statement beyond the loop 
or a switch. With loops, break can be used to force an early exit from the loop, or 
to implement a loop with a test to exit in the middle of the loop body. A break within 
a loop should always be protected within an if statement, this provides the test to control 
the loop exit condition. 
C Examples Using the break Statement


The following example shows a break statement in the action part of a for statement. 
If the ith element of the array string is equal to '\0', the break statement causes 
the for statement to end. 

1. A break statement  in for loop example segment:

	for (i = 0; i <5; i++) { if (string[i]="=" '\0') break; length++; } 

The following is an equivalent for statement, if string does not contain any embedded null characters:

	for (i = 0; (i <5)&& (string[i] !="\0" ); i++) { length++; } 

The following example shows a break statement in a nested iterative statement. The outer loop goes through an array of pointers to strings. The inner loop examines each character of the string. When the break statement is processed, the inner loop ends and control returns to the outer loop.

2. A break statement in a complete nested for loop example:

	/**
	**  This program counts the characters in the strings that are
	**  part of an array of pointers to characters.  The count stops
	**  when one of the digits 0 through 9 is encountered
	**  and resumes at the beginning of the next string.
	**/

	#include 
	#define  SIZE  3

	int main(void)
	{
		static char *strings[SIZE] = { "ab", "c5d", "e5" };
		int i;
		int letter_count = 0;
		char *pointer;

		for (i = 0; i ='0' && *pointer <= '9' ) break; letter_count++; } printf("letter count="%d\n" ," letter_count); } The program produces the following output: letter count="4" 

3. A break statement in a selective statement block example:
The following example is a switch statement that contains several break statements. Each break statement indicates the end of a specific clause and ends the switch statement.

	#include 

	enum {morning, afternoon, evening} timeofday = morning;

	int main(void) {

	switch (timeofday) {
	case (morning):
          printf("Good Morning\n");
          break;

	case (evening):
          printf("Good Evening\n");
          break;

	default:
          printf("Good Day, eh\n");
		}
	}


The continue Statement

This is similar to break but is less frequently encountered. It only works within 
loops where its effect is to force an immediate jump to the loop control statement.

In a while loop, jump to the test statement. In a do while loop, jump to the test statement. In a for loop, jump to the test, and the iteration.

Like a break, continue should be protected by an if statement. You are unlikely to use it very frequently.

C Examples Using the continue Statement
In an iteration loop the continue statement forces the start of the next iteration or loop-continuation of the inner most while, do...while or for loop. The following two examples how a continue statement in a for statement.

1. A continue statement  in for loop example segment:

	for(x = 10; x > 0; x = x - 1) {
		if(x > 5)
		continue;
	printf("x = %d\n",x);
	}

This will only allow the numbers 5,4,3,2 and 1 to be printed out.  The next iteration resumes
only after evaluation of the current iteration expr3.

2. A continue  statement in  a complete for loop example:
   The continue statement causes processing to skip over those elements of the array 
   rates that have values less than or equal to 1.

	#include 
	#define  SIZE  5

	int main(void)
	{
		int i;
		static float rates[SIZE] = { 1.45, 0.05, 1.88, 2.00, 0.75 };

		printf("Rates over 1.00\n");
		for (i = 0; i continue statement  in while loop example segment:

	while (a  4.5)
		continue;
	printf("\n the value of a is %f", a);
	}
	c = d;

4. A continue statement in a complete nested loop example:

   The following example shows a continue statement in a nested loop. When the inner 
   loop encounters a number in the array strings, then that iteration of the loop ends. 
   Processing continues with the third expression of the inner loop. The inner loop ends 
   when the '\0' escape sequence is encountered.

	/**
	** This program counts the characters in strings that are part
	** of an array of pointers to characters.  The count excludes
	** the digits 0 through 9.
	**/

	#include 
	#define  SIZE  3

	int main(void)
	{
		static char *strings[SIZE] = { "ab", "c5d", "e5" };
		int i;
		int letter_count = 0;
		char *pointer;
		for (i = 0; i = '0' && *pointer <= '9') continue; letter_count++; } printf("letter count="%d\n"," letter_count); } The program produces the following output: letter count="5" 

2. The goto Statement

C has a goto statement which permits unstructured jumps to be made. Though it 
sometimes can be very powerful, it is not often recommended for lack of better control. 
The following is the syntax of goto statement and an c example:
goto name;
. . .
name: statement

The goto keyword transfers control directly to the statement specified by the label 
name.

C Example

In this example, a goto statement transfers control to the point labeled stop when 
i equals 5.

// Example of the goto statement
void main()
{
   int i, j;
   
   for ( i = 0; i <10; i++) { printf( "Outer loop executing. i="%d\n"," i ); for ( j="0;" j < 3; j++ ) { printf( " Inner loop executing. j="%d\n"," j ); if ( i="=" 5 ) goto stop; } } /* This message does not print: */ printf( "Loop exited. i="%d\n"," i ); stop: printf( "Jumped to stop. i="%d\n"," i ); } 

Points On GoTo Statement