goto_statements - The goto Statement: Legitimate Uses The...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
The goto Statement: Legitimate Uses The goto statement is, deservedly, much maligned. However, there remain at least two legitimate uses in contemporary programming languages like C++. Exiting Nested Loops Chapter 3 introduced the break and continue statements, which are used in conjunction with loops. The general usage looks like one of the following: for (int i = 0; i < counter; i++) { . . . if (condition) break; . . . } for (int i = 0; i < counter; i++) { . . . if (condition) continue; . . . } When a break statement is executed, all statements between the break and the end of the loop are skipped. Execution resumes with the first statement following the end of the loop. When a continue statement is executed, all statements between the continue and the end of the loop are skipped. However, execution begins at the top of the loop: if it is a for-loop, the increment expression is executed, then (for all loops) the test is evaluated and if it is true, the next iteration of the loop takes place. However, the break and continue statements only work for one level of loop.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 2

goto_statements - The goto Statement: Legitimate Uses The...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online