break/continue; goto (avoid); scope; exceptions vs guard checks
Use break and continue correctly, understand why goto is discouraged, apply scope rules safely, and choose between exceptions and guard checks.
Lesson overview
Goal: Control loop flow with break/continue, avoid goto, respect variable scope, and pick between exceptions and guard checks.
break in action
break exits the loop immediately.
using System;
public class Program
{
public static void Main(string[] args)
{
for (int i = 1; i <= 10; i = i + 1)
{
if (i == 4)
{
Console.WriteLine("Stop at 4");
break; // exit loop completely
}
Console.WriteLine("i = " + i);
}
}
}
All lessons in this course
- if/else, switch (classic), loops: for, while, do, foreach
- break/continue; goto (avoid); scope; exceptions vs guard checks
- Simple diagnostics: exceptions vs guard checks