0% found this document useful (0 votes)
35 views1 page

Continue Loop Example: #Include

The continue statement allows a program to skip the rest of the current loop iteration and jump to the next iteration. For example, using continue in a for loop counting down from 10 to 1 would skip printing the number 5. The goto statement allows an unconditional jump to a labeled point in the program, ignoring block structure. While generally discouraged, goto can be used to rewrite a countdown loop to jump to a label at the end of each iteration.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

Continue Loop Example: #Include

The continue statement allows a program to skip the rest of the current loop iteration and jump to the next iteration. For example, using continue in a for loop counting down from 10 to 1 would skip printing the number 5. The goto statement allows an unconditional jump to a labeled point in the program, ignoring block structure. While generally discouraged, goto can be used to rewrite a countdown loop to jump to a label at the end of each iteration.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

The continue statement

The continue statement causes the program to skip the rest of the loop in the current iteration,
as if the end of the statement block had been reached, causing it to jump to the start of the
following iteration. For example, let's skip number 5 in our countdown:
1
2
3
4
5
6
7
8
9
10
11
12
// continue loop example
#include <iostream>
using namespace std;

int main ()
{
for (int n=10; n>0; n--) {
if (n==5) continue;
cout << n << ", ";
}
cout << "liftoff!\n";
}
10, 9, 8, 7, 6, 4, 3, 2, 1, liftoff!


The goto statement
goto allows to make an absolute jump to another point in the program. This unconditional jump
ignores nesting levels, and does not cause any automatic stack unwinding. Therefore, it is a
feature to use with care, and preferably within the same block of statements, especially in the
presence of local variables.

The destination point is identified by a label, which is then used as an argument for
the goto statement. A label is made of a valid identifier followed by a colon (:).

goto is generally deemed a low-level feature, with no particular use cases in modern higher-level
programming paradigms generally used with C++. But, just as an example, here is a version of
our countdown loop using goto:
1
2
3
4
5
6
7
8
9
10
11
12
13
// goto loop example
#include <iostream>
using namespace std;

int main ()
{
int n=10;
mylabel:
cout << n << ", ";
n--;
if (n>0) goto mylabel;
cout << "liftoff!\n";
}
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, liftoff!

You might also like