break/continue、goto(使用を避ける)、スコープ、例外とガードチェック
breakとcontinueを正しく使い、gotoが推奨されない理由を理解します。スコープのルールを安全に適用し、例外とガードチェックを使い分けます。
「break/continue、goto(使用を避ける)、スコープ、例外とガードチェック」はCoddyKit上の無料C# Academyレッスンです。 これはレッスン2/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはC# Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 C# Academyコースには全3レッスンが含まれています。
レッスン概要
目標:break/continue でループの流れを制御し、goto を避け、変数の スコープを適切に扱い、例外とガードチェックを使い分けます。
break の動作
break はループを直ちに終了します。
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 の動作
continue は現在の反復をスキップし、次の反復に進みます。
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);
}
}
}
goto を避ける
goto はスパゲッティ状のフローを作り出します。代わりに構造化されたループ(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);
}
}
}
スコープの基礎
スコープは中かっこ { } で定義されます。使う場所の近くで変数を宣言し、入れ子のブロックで同じ名前を再利用しないようにします。
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);
}
}
}
診断の基礎
想定される無効な入力にはガードチェックを使い、例外的な状態には例外を使います。
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 と continue
まとめ
まとめ:ループを終了するには break、反復をスキップするには continue を使います。goto は避け、変数を適切な スコープに保ちます。想定される無効な入力にはガードチェックを、例外的な状態には例外を優先します。
よくある質問
「break/continue、goto(使用を避ける)、スコープ、例外とガードチェック」レッスンは無料ですか?
はい。「break/continue、goto(使用を避ける)、スコープ、例外とガードチェック」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、C# Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 C# Academyコースには全3レッスンが含まれています。
「break/continue、goto(使用を避ける)、スコープ、例外とガードチェック」で何を学びますか?
breakとcontinueを正しく使い、gotoが推奨されない理由を理解します。スコープのルールを安全に適用し、例外とガードチェックを使い分けます。 ブラウザで直接実行するハンズオンコードでC# Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
C# Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのC# Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/3です。
「break/continue、goto(使用を避ける)、スコープ、例外とガードチェック」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このC# Academyレッスンでコードを書いて実行できますか?
はい。すべてのC# Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- if/else、switch(従来型)、ループ:for、while、do、foreach
- break/continue、goto(使用を避ける)、スコープ、例外とガードチェック
- 簡単な診断:例外とガードチェック