お遍路さんのプログラム言語備忘録

繰り返し処理

処理内容
1から10まで値を順次処理。
値を3で割った余りをチェックし、余りが1以外の場合に繰り返しを抜ける。
但し、3以下の値はチェックをせずに次の値の処理に移動する。
処理結果
1を3で割った余り:1
continue
2を3で割った余り:2
continue
3を3で割った余り:0
continue
4を3で割った余り:1
ループ
5を3で割った余り:2
break

VisualC#繰り返し処理

確認環境Microsoft Visual Studio Community 2019Version 16.4.3.NET Core 3.1
for(初期化式; 条件式; 更新式)
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
public void Pf_For()
{
    for (int i = 1; i <= 10; i++)
    {
        // iを3で割った余りを求める
        int iDivided = i % 3;
        Console.WriteLine(string.Format("{0}を3で割った余り:{1}", i, iDivided));
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            Console.WriteLine("continue");
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            Console.WriteLine("break");
            break;
        }
        Console.WriteLine("ループ");
    }
}
foreach(変数宣言 in オブジェクト)
{
    繰り返す処理文    // オブジェクトの各要素に対して1回ずつ処理を行う。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
public void Pf_Foreach()
{
    int[] arrI = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    foreach (int i in arrI)
    {
        // iを3で割った余りを求める
        int iDivided = i % 3;
        Console.WriteLine(string.Format("{0}を3で割った余り:{1}", i, iDivided));
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            Console.WriteLine("continue");
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            Console.WriteLine("break");
            break;
        }
        Console.WriteLine("ループ");
    }
}
do
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
while(条件式);
次の繰り返しへcontinue繰り返しを抜けるbreak
public void Pf_DoWhile()
{
    int i = 0;
    do
    {
        ++i;
        // iを3で割った余りを求める
        int iDivided = i % 3;
        Console.WriteLine(string.Format("{0}を3で割った余り:{1}", i, iDivided));
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            Console.WriteLine("continue");
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            Console.WriteLine("break");
            break;
        }
        Console.WriteLine("ループ");
    }
    while (i < 10);
}
while(条件式)
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
public void Pf_While()
{
    int i = 0;
    while (i < 10)
    {
        ++i;
        // iを3で割った余りを求める
        int iDivided = i % 3;
        Console.WriteLine(string.Format("{0}を3で割った余り:{1}", i, iDivided));
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            Console.WriteLine("continue");
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            Console.WriteLine("break");
            break;
        }
        Console.WriteLine("ループ");
    }
}
ページの先頭に戻る

C++の繰り返し処理

確認環境Microsoft Visual Studio Community 2019Version 16.4.3.ISO C++17 標準 (std:c++17)
for (初期化式; 条件式; 更新式)
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
void Pf_For()
{
    for (int i = 1; i <= 10; i++)
    {
        // iを3で割った余りを求める
        int iDivided = i % 3;
        printf("%iを3で割った余り:%i\n", i, iDivided);
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            std::cout << "continue" << std::endl;
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            std::cout << "break" << std::endl;
            break;
        }
        std::cout << "ループ" << std::endl;
    }
}
do
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
while (条件式);
次の繰り返しへcontinue繰り返しを抜けるbreak
void Pf_DoWhile()
{
    int i = 0;
    do
    {
        ++i;
        // iを3で割った余りを求める
        int iDivided = i % 3;
        printf("%iを3で割った余り:%i\n",i ,iDivided);
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            std::cout << "continue" << std::endl;
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            std::cout << "break" << std::endl;
            break;
        }
        std::cout << "ループ" << std::endl;
    } while (i < 10);
}
while (条件式)
{
    繰り返す処理文    // 条件式が真の間繰り返される。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
void Pf_While()
{
    int i = 0;
    while (i < 10)
    {
        ++i;
        // iを3で割った余りを求める
        int iDivided = i % 3;
        printf("%iを3で割った余り:%i\n", i, iDivided);
        // iが3以下の時は次ループ
        if (i <= 3)
        {
            std::cout << "continue" << std::endl;
            continue;
        }
        // 余りが1以外の時はループを抜ける
        if (iDivided != 1)
        {
            std::cout << "break" << std::endl;
            break;
        }
        std::cout << "ループ" << std::endl;
    }
}
ページの先頭に戻る 

Pythonの繰り返し処理

確認環境PythonVersion 3.7.2
for 変数 in オブジェクト:
    繰り返す処理文    # オブジェクトの各要素に対して1回ずつ処理を行う。
次の繰り返しへcontinue繰り返しを抜けるbreak
def Pf_For():
  for i in range(1, 11):
    # iを3で割った余りを求める
    iDivided = i % 3;
    print("{0}を3で割った余り:{1}".format(i, iDivided))
    # iが3以下の時は次ループ
    if i <= 3:
      print("continue")
      continue
    # 余りが1以外の時はループを抜ける
    if iDivided != 1:
      print("break")
      break
    print("ループ")
while 条件式:
    繰り返す処理文    # 条件式が真の間繰り返される。
次の繰り返しへcontinue繰り返しを抜けるbreak
def Pf_While():
  i = 0;
  while i < 10:
    i += 1;
    # iを3で割った余りを求める
    iDivided = i % 3;
    print("{0}を3で割った余り:{1}".format(i, iDivided))
    # iが3以下の時は次ループ
    if i <= 3:
      print("continue")
      continue
    # 余りが1以外の時はループを抜ける
    if iDivided != 1:
      print("break")
      break
    print("ループ")
ページの先頭に戻る 

VisualBasic.Netの繰り返し処理

確認環境Microsoft Visual Studio Community 2019Version 16.4.3.NET Core 3.1
For 変数=初期値 To 終了値 Step 増加値
    繰り返す処理文    ' 条件式が真の間繰り返される。
Next 変数
次の繰り返しへContinue For繰り返しを抜けるExit For
Public Sub Pf_For()
    Dim i As Integer
    Dim iDivided As Integer
    For i = 1 To 10 Step 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue For
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit For
        End If
        Console.WriteLine("ループ")
    Next i
End Sub
For Each 変数宣言 in オブジェクト
    繰り返す処理文    ' オブジェクトの各要素に対して1回ずつ処理を行う。
Next 変数
次の繰り返しへContinue For繰り返しを抜けるExit For
Public Sub Pf_ForEach()
    Dim iDivided As Integer
    Dim arrI As Integer() = New Integer() {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    For Each i As Integer In arrI
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue For
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit For
        End If
        Console.WriteLine("ループ")
    Next i
End Sub
Do While 条件式
    繰り返す処理文    ' 条件式が真の間繰り返される。
Loop
次の繰り返しへContinue Do繰り返しを抜けるExit Do
Public Sub Pf_DoWhileLoop()
    Dim i As Integer = 0
    Dim iDivided As Integer
    Do While i < 10
        i += 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue Do
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit Do
        End If
        Console.WriteLine("ループ")
    Loop
End Sub
Do
    繰り返す処理文    ' 条件式が真の間繰り返される。
Loop While 条件式
次の繰り返しへContinue Do繰り返しを抜けるExit Do
Public Sub Pf_DoLoopWhile()
    Dim i As Integer = 0
    Dim iDivided As Integer
    Do
        i += 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue Do
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit Do
        End If
        Console.WriteLine("ループ")
    Loop While i < 10
End Sub
Do Until 条件式
    繰り返す処理文    ' 条件式が虚の間繰り返される。
Loop
次の繰り返しへContinue Do繰り返しを抜けるExit Do
Public Sub Pf_DoUntilLoop()
    Dim i As Integer = 0
    Dim iDivided As Integer
    Do Until i >= 10
        i += 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue Do
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit Do
        End If
        Console.WriteLine("ループ")
    Loop
End Sub
Do
    繰り返す処理文    ' 条件式が虚の間繰り返される。
Loop Until 条件式
次の繰り返しへContinue Do繰り返しを抜けるExit Do
Public Sub Pf_DoLoopUntil()
    Dim i As Integer = 0
    Dim iDivided As Integer
    Do
        i += 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue Do
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit Do
        End If
        Console.WriteLine("ループ")
    Loop Until i >= 10
End Sub
While 条件式
    繰り返す処理文    ' 条件式が真の間繰り返される。
End While
次の繰り返しへContinue While繰り返しを抜けるExit While
Public Sub Pf_While()
    Dim i As Integer = 0
    Dim iDivided As Integer
    While i < 10
        i += 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        Console.WriteLine(String.Format("{0}を3で割った余り:{1}", i, iDivided))
        ' iが3以下の時は次ループ
        If i <= 3 Then
            Console.WriteLine("continue")
            Continue While
        End If
        ' 余りが1以外の時はループを抜ける
        If iDivided <> 1 Then
            Console.WriteLine("break")
            Exit While
        End If
        Console.WriteLine("ループ")
    End While
End Sub
ページの先頭に戻る 

JavaScript/NodeJsの繰り返し処理

確認環境NodeJsVersion 12.14.1
for (  初期化式;  条件式;  増減値  ){
    繰り返す処理文    // 条件式が真の間繰り返される。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
for ( var i = 1; i <= 10; i++ ) {
  // iを3で割った余りを求める
  var iDivided = i % 3;
  console.log (i + 'を3で割った余り:' + iDivided);
  // iが3以下の時は次ループ
  if (i <= 3) {
    console.log ("continue");
    continue;
  }
  // 余りが1以外の時はループを抜ける
  if (iDivided != 1) {
    console.log ("break");
    break;
  }
}
for (変数宣言 of 配列) {
  繰り返す処理文    // オブジェクトの各要素に対して1回ずつ処理を行う。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
var arrI = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for ( var i of arrI) {
  // iを3で割った余りを求める
  var iDivided = i % 3;
  console.log (i + 'を3で割った余り:' + iDivided);
  // iが3以下の時は次ループ
  if (i <= 3) {
    console.log ("continue");
    continue;
  }
  // 余りが1以外の時はループを抜ける
  if (iDivided != 1)
  {
    console.log ("break");
    break;
  }
}
for (変数宣言 in オブジェクト) {
  繰り返す処理文    // オブジェクトの各要素に対して1回ずつ処理を行う。
}
次の繰り返しへcontinue繰り返しを抜けるbreak
var arrI = { item1: 1, item2: 2, item3: 3, item4: 4, item5: 5,
             item6: 6, item7: 7, item8: 8, item9: 9, item10: 10 };
for ( var key in arrI) {
  // itemを3で割った余りを求める
  var iDivided = arrI[key] % 3;
  console.log (arrI[key] + 'を3で割った余り:' + iDivided);
  // itemが3以下の時は次ループ
  if (arrI[key] <= 3) {
    console.log ("continue");
    continue;
  }
  // 余りが1以外の時はループを抜ける
  if (iDivided != 1) {
    console.log ("break");
    break;
  }
}
array.forEach( コールバック関数 ) {
  繰り返す処理文    // オブジェクトの各要素に対して1回ずつ処理を行う。
}
次の繰り返しへ無し繰り返しを抜ける無し
var arrI = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var bBreak = false;
arrI.forEach ( function( i ) {
  if (bBreak === false) {
    // iを3で割った余りを求める
    var iDivided = i % 3;
    console.log (i + 'を3で割った余り:' + iDivided);
    // iが3以下の時は判断無し
    if (i <= 3) {
      console.log ("continue");
      return;
    } else
    // 余りが1以外の時はループを抜ける
    if (iDivided != 1) {
      console.log ("break");
      bBreak = true;
      return;
    }
  }
})
ページの先頭に戻る 

VBA(VisualBasicScript)の繰り返し処理

確認環境Microsoft Excel for Office 365 MSO:64ビットバージョン 1911ビルド:12228.2364 クイック実行
For 変数=初期値 To 終了値 Step 増加値
    繰り返す処理文    ' 条件式が真の間繰り返される。
Next 変数
次の繰り返しへ無し繰り返しを抜けるExit For
Public Sub Pf_For()
    Dim i As Integer
    Dim iDivided As Integer
    Dim iRow As Integer
    For i = 1 To 10 Step 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit For
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Next i
End Sub
For Each 変数宣言 in オブジェクト コレクション
    繰り返す処理文    ' オブジェクト コレクションの各要素に対して1回ずつ処理を行う。
Next 変数
次の繰り返しへ無し繰り返しを抜けるExit For
Public Sub Pf_ForEach()
    Dim i As Variant
    Dim iDivided As Integer
    Dim iRow As Integer
    Dim arrI() As Variant
    arrI = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    For Each i In arrI
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                    iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit For
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Next i
End Sub
Do While 条件式
    繰り返す処理文    ' 条件式が真の間繰り返される。
Loop
次の繰り返しへ無し繰り返しを抜けるExit Do
Public Sub Pf_DoWhileLoop()
    Dim i As Integer
    Dim iDivided As Integer
    i = 0
    Do While i < 10
        i = i + 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit Do
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Loop
End Sub
Do
    繰り返す処理文    ' 条件式が真の間繰り返される。
Loop While 条件式
次の繰り返しへ無し繰り返しを抜けるExit Do
Public Sub Pf_DoLoopWhile()
    Dim i As Integer
    Dim iDivided As Integer
    i = 0
    Do
        i = i + 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit Do
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Loop While i < 10
End Sub
Do Until 条件式
    繰り返す処理文    ' 条件式が虚の間繰り返される。
Loop
次の繰り返しへ無し繰り返しを抜けるExit Do
Public Sub Pf_DoUntilLoop()
    Dim i As Integer
    Dim iDivided As Integer
    i = 0
    Do Until i >= 10
        i = i + 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit Do
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Loop
End Sub
Do
    繰り返す処理文    ' 条件式が虚の間繰り返される。
Loop Until 条件式
次の繰り返しへ無し繰り返しを抜けるExit Do
Public Sub Pf_DoLoopUntil()
    Dim i As Integer
    Dim iDivided As Integer
    i = 0
    Do
        i = i + 1
        ' iを3で割った余りを求める
        iDivided = i Mod 3
        iRow = iRow + 1: Cells(iRow, 1).Value = i & "を3で割った余り:" & iDivided
        ' iが3以下の時は次ループ
        If i <= 3 Then
            iRow = iRow + 1: Cells(iRow, 1).Value = "continue"
        Else
            ' 余りが1以外の時はループを抜ける
            If iDivided <> 1 Then
                iRow = iRow + 1: Cells(iRow, 1).Value = "break"
                Exit Do
            End If
            iRow = iRow + 1: Cells(iRow, 1).Value = "ループ"
        End If
    Loop Until i >= 10
End Sub
ページの先頭に戻る 
page top