while do~ while C# 入門
while
whileはある条件を満たしているとき実行を繰り返します。
while (条件)
{
処理
}
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 = 1;
while (num < 3)
{
Console.WriteLine(num);
num++;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
実行結果は
1
2
になります。
vb.netだと
While End Whileと
Do While Loopあり同じ動作をします。
While (条件)
処理
End While
Do While (条件)
処理
Loop
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num As Integer = 1
While (num < 3)
Console.WriteLine(num)
num += 1
End While
End Sub
End Class
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num As Integer = 1
Do While (num < 3)
Console.WriteLine(num)
num += 1
Loop
End Sub
End Class
実行結果は2つとも
1
2
になります。
vb.netには
Do Until (条件)
処理
Loop
Untiは指定の条件になった時終了します。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num As Integer = 4
Do Until (num < 3)
Console.WriteLine(num)
num += 1
Loop
End Sub
End Class
実行結果は
4からIntegerの境界値エラーが出るまで実行されます。
確認はしてませんが。
変数の初期値が3未満の時は終了条件を満たしていますので何も処理されず終了します。
do~ while
do~ whileは処理を実行してから繰り返します。
条件を満たしていなくても必ず1回は実行されます。
do
{
処理
} while (条件;
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 = 1;
do
{
Console.WriteLine(num);
num++;
} while (num < 3);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
実行結果は
1
2
になります。
もし変数の値が条件を満たしてない場合実行されます。
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 = 51;
do
{
Console.WriteLine(num);
num++;
} while (num < 3);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
実行結果は
51になります。
vb.netだと
Do
処理
Loop While (条件)
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num As Integer = 1
Do
Console.WriteLine(num)
num += 1
Loop While (num < 3)
End Sub
End Class
実行結果は
1
3
になります。
また辺巣の初期値が違う場合1回だけ実行します。
Do Loop Until 文
Do
処理
Loop Until (条件)
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num As Integer = 1
Do
Console.WriteLine(num)
num += 1
Loop Until (num < 3)
End Sub
End Class
実行結果は
1です。
終了条件を満たしているので1回だけ実行されます。
変数の初期値が条件を満たしていない場合は
その初期値から1を足す処理を繰り返します。