C#for foreach

for foreach C# 入門

for 繰り返し

for文は繰り返す時に使います。
カウンタとして使う時が多いですね。

for (初期値; 実行条件; インクリメント)
{
処理
}

サンプルソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int i;

for (i = 0; i < 4; i++)
{
Console.WriteLine(i);
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は
0
1
2
3
になります。

初期値がiがゼロから始めることを指定しています。
実行条件が4未満になっています。

ちなみに
vb.netでは
同じ処理を
for i=0 to 3
Console.WriteLine(i)
next
で書けますね。

変数iはループの外で宣言しているためスコープが広くなってしまってます。

この様なことが嫌なら
初期値の所で宣言すればいいでしょう。

具体的には
for (int i = 0; i < 4; i++)
{ Console.WriteLine(i); }
vb.netでも同じですね。

for i as integer = 0 to 3
Console.WriteLine(i) next

またvb.netではstepができます。
for i as integer = 0 to 3 step 2
Console.WriteLine(i) next

これで2ずつiが増えます。
カウントダウンしたいときは

ソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { for (int i = 3; i >= 0; i–)
{
Console.WriteLine(i);
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は
3
2
1
0
になります。

vb.netではカウントダウンをどう作ればいいでしょうか?

答えはstep を-1にします。
ソース
For i As Integer = 3 To 0 Step -1

MsgBox(i)
Next
3
2
1
0
になります。

for 二重ループ

二重ループはfor文を入れ子にして変数を二つ使います。

サンプルソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.WriteLine(i);
}
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は

0
0
0
0
1
1
1
1
2
2
2
2
3
3
3
3
になります。

ループの流れを意識して欲しいのでこの様に書きました。
この場合はiだけ出力してます。

まずiの値は0ですね。
それからjのループに入ります。
ここでjが四回繰り返します。
このループを抜けると
iが1になります。
またjの繰り返しが始まります。

よくある掛け算のサンプルだと

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

for (int i = 1; i < 4; i++)
{
for (int j = 1; j < 4; j++)
{
Console.WriteLine(i + “*” + j);

}
}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は
1*1
1*2
1*3
2*1
2*2
2*3
3*1
3*2
3*3

何となく分かりましたか。

foreach

foreach (型 変数名 in 配列かコレクション)

foreach は配列やコレクションを出力処理するときに使ったりしますね。

サンプルソース

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int[] num = new int[3];

foreach (int i in num)
{

Console.WriteLine(i);

}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は
0
0
0
になります。

配列の初期値が0だったのでそれを出力しました。
iに1ずつ増やして代入したいときはインクリメントを書きたいところですが
それは出来ません。

どうすればいいでしょうか。
インクリメント用に変数jを使いましょう。

サンプルソース
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

int[] num = new int[3];

int j = 0;

foreach (int i in num)
{

Console.WriteLine(num[i] = j);

j++;

}

}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

実行結果は
0
1
2
になります。

vbでも同じです。
構文が
for Each 変数名 As データ型 In 配列かコレクション

next

for Eachはforの前にスベースがあります。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

Translate »