Locked lesson.
About this lesson
In this lesson, we explain how to Break and Continue loops.
Exercise files
Download this lesson’s related exercise files.
38 - Loops Break and Continue.docx60.9 KB 38 - Loops Break and Continue SOLUTION.docx
58.9 KB
Quick reference
Loops: Break and Continue
Break allows us to break out of a loop. Continue allows us to break one iteration of the loop and then continue on.
When to use
Use these when you need to break out of a loop and/or continue on afterward.
Instructions
Break:
int x = 8;
while (x >= 1 )
{
if (x == 4)
break;
Console.WriteLine(x);
x--;
}
Continue:
int x = 8;
while (x >= 1 )
{
if (x == 4)
{
x--;
continue;
}
Console.WriteLine(x);
x--;
}
Hints & tips
- Break allows you to break out of a loop
- Continue allows you to break one iteration of the loop and then continue on
Lesson notes are only available for subscribers.