Introduction to Programming and C++

Contents Previous Topic Next Topic

C++ has three types of loops which we have examined:

  1. while loops,
  2. for loops, and
  3. do-while loops.

With the first two loops, before the body of the loop is run, a test is checked to determine whether the loop should continue running. If the test returns false, the loop halts and the next statement after the loop is executed next. A do-while loop is similar, only the test to determine if the loop should continue running is performed after each time the body of the loop is run.

In some cases, it may be convenient to short-cut certain aspects of the loop: for example, it may be desirable to terminate a loop early, or perhaps, it may be desirable to skip the remaining body of the loop and go back to the test. These two

As a bonus, we will see an excellent use of the goto statement in C++.

The break Statement

The continue Statement

The goto Statement

In the early days of programming, the goto statement allowed programmers to write absolutely atrocious spaghetti code, where the code would jump from one location to another. The first person to publicly realize this was Edsger W. Dijkstra's in his 1968 letter to the editor: Go To Statement Considered Harmful. This started a backlash against any use of the goto statement and some individuals appear to have adopted an almost religious crusade against its use, even when it is the natural tool.

Suppose you have two nested while loops, as demonstrated in Code Fragment 3.

Code Fragment 3. Breaking out of nested while loops.

	while ( /* some condition */ ) {
		// do something

		while ( /* another condition */ ) {
			// do a second thing

			if ( /* another condition */ ) {
				(1)
				// realize some condition has been satisfied
				// need to jump to (3)
			}

			// do a third thing
		}

		(2)

		// do a forth thing
	}

	(3)
	// end of outer loop

A break; at (1) would only jump to (2). A member of the goto-less cult may suggest that you should write this as:

Code Fragment 4. A goto-less solution to the problem posed in Code Fragment 3.

	while ( /* some condition */ ) {
		// do something
		bool flag = false;

		while ( /* another condition */ ) {
			// do a second thing

			if ( /* another condition */ ) {
				flag = true;
				break;
			}

			// do a third thing
		}

		if ( flag ) {
			break;
		}

		// do a forth thing
	}

	// end of outer loop

A more natural implementation is shown in Code Fragment 5 where a goto jumps to the appropriate location.

Code Fragment 5. A reasonable solution to the problem posed in Code Fragment 3.

	while ( /* some condition */ ) {
		// do something

		while ( /* another condition */ ) {
			// do a second thing

			if ( /* another condition */ ) {
				goto END;
			}

			// do a third thing
		}

		// do a forth thing
	}

	END:
	// end of outer loop

The END: is called a label and may be (almost) any name followed by a colon.

Questions

3. The Perl programming language has a redo command which jumps back to the start of a loop without performing the test. How would you implement this in C++?

4. The intercal programming language has a comefrom statement which works exactly in a similar manner as the goto statement. Discuss the programming issues with the comefrom statement.


Contents Previous Topic Top Next Topic