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.
break/continue; goto (avoid); scope; exceptions vs guard checks is a free C# Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}
}
}
continue in action
continue skips the current iteration and goes to the next.
using System;
public class Program
{
public static void Main(string[] args)
{
for (int i = 1; i <= 5; i = i + 1)
{
if (i % 2 == 0)
{
continue; // skip even numbers
}
Console.WriteLine("Odd: " + i);
}
}
}
Avoiding goto
goto creates spaghetti flow; use structured loops instead (while/for/foreach).
using System;
public class Program
{
public static void Main(string[] args)
{
// Goto works but harms readability; prefer structured loops.
int i = 0;
start:
i = i + 1;
Console.WriteLine("i = " + i);
if (i < 3)
{
goto start; // avoid using goto in real code
}
// Prefer: while loop (clear intent)
int j = 0;
while (j < 3)
{
j = j + 1;
Console.WriteLine("j = " + j);
}
}
}
Scope basics
Scope is defined by braces { }. Declare variables close to use and avoid name reuse in nested blocks.
using System;
public class Program
{
public static void Main(string[] args)
{
int x = 10; // outer scope
if (x > 0)
{
int y = 5; // inner scope
Console.WriteLine("x + y = " + (x + y));
}
// y is not visible here; this would not compile:
// Console.WriteLine(y);
// Shadowing (avoid reusing names in nested scopes)
int value = 1;
{
int valueInner = 2; // use a new name instead of shadowing
Console.WriteLine("value=" + value + ", inner=" + valueInner);
}
}
}
Diagnostics basics
Use guard checks for expected invalid input; use exceptions for exceptional states.
using System;
public class Program
{
// Guard check: return a bool and an out parameter (no exception on normal invalid input).
public static bool TryDivide(int x, int y, out int result)
{
if (y == 0)
{
result = 0;
return false; // guard fail
}
result = x / y;
return true;
}
// Exceptional state: throw when something truly exceptional happens.
public static int PriceWithTax(int price, int ratePercent)
{
if (price < 0)
{
throw new ArgumentOutOfRangeException("price");
}
if (ratePercent < 0 || ratePercent > 100)
{
throw new ArgumentOutOfRangeException("ratePercent");
}
return price + (price * ratePercent / 100);
}
public static void Main(string[] args)
{
int r;
bool ok = TryDivide(10, 0, out r);
Console.WriteLine("TryDivide ok=" + ok + " result=" + r);
try
{
int total = PriceWithTax(100, 18);
Console.WriteLine("Total=" + total);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
break vs continue
Recap
Recap: Use break to exit and continue to skip an iteration; avoid goto; keep variables in the right scope; prefer guard checks for expected invalid input and exceptions for exceptional states.
Frequently asked questions
Is the “break/continue; goto (avoid); scope; exceptions vs guard checks” lesson free?
Yes — the full text of “break/continue; goto (avoid); scope; exceptions vs guard checks” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “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. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “break/continue; goto (avoid); scope; exceptions vs guard checks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
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