VB.NETのWhile文のサンプルです。条件で処理の繰り返しを制御します。
目次
While文 | While文 |
Exit | ループを途中で終了する(Exit) |
入れ子のときにExitを使用する | |
Continue | ループで処理をスキップする(Continue) |
入れ子のときにContinueを使用する | |
do...while文 | do...while文にあたる書き方 |
演算子 | 比較演算子 |
While文
While 条件 実行される処理 End While |
- 条件がtrueの間、処理を繰り返します。
- 最初の条件の判定でfalseの場合、ループは1回も実行されません。
- 処理に、条件をfalseにするロジックがないと無限ループになるので注意が必要です。
- 以下は、MicrosoftのWhile (Visual Basic)のリンクです。
https://docs.microsoft.com/ja-jp/dotnet/visual-basic/language-reference/statements/while-end-while-statement
While文のサンプルです。
Module Module1
Sub Main()
Dim i As Integer = 0 '変数
While i < 3
Console.WriteLine(i) '0 1 2が出力される
i = i + 1
End While
End Sub
End Module
5行目は、値が3より小さい間、処理を繰り返します。
7行目は、条件になる値を加算しています。
ループを途中で終了する(Exit)
ループを途中で終了するサンプルです。
Module Module1
Sub Main()
Dim i As Integer = 0 '変数
While i < 5
If (i = 3) Then
Exit While
End If
Console.WriteLine(i) '0 1 2が出力される
i = i + 1
End While
End Sub
End Module
7行目のExit Whileは、while文を抜けます。
そのため9行目の文字列の出力は0,1,2のみ出力されます。
入れ子のときにExitを使用する
入れ子のときにExitを使用するサンプルです。
Module Module1
Sub Main()
Dim i As Integer = 0 '変数
Dim j As Integer = 0 '変数
While i < 2
While j < 2
If (j = 0) Then
Exit While
End If
Console.WriteLine("j=" & j) '出力なし
j = j + 1
End While
Console.WriteLine("i=" & i) 'i=0 i=1が出力される
i = i + 1
End While
End Sub
End Module
6,7行目にWhile文があり入れ子になっています。
9行目のExit Whileは、内側のWhile文のみ抜けます。次は14行目にいきます。
12行目は実行されないのでjの値は常に0です。
ループで処理をスキップする(Continue)
ループで処理をスキップするサンプルです。
Module Module1
Sub Main()
Dim i As Integer = 0 '変数
While i < 5
If (i = 3) Then
i = i + 1
Continue While
End If
Console.WriteLine(i) '0,1,2,4が出力される
i = i + 1
End While
End Sub
End Module
8行目のcontinueは、次は5行目を実行します。そのため10行目の文字列の出力は実行されません。
このサンプルでは7行目の加算がないと無限ループになるので注意して下さい。
入れ子のときにContinueを使用する
入れ子のときにContinueを使用するサンプルです。
Module Module1
Sub Main()
Dim i As Integer = 0 '変数
Dim j As Integer = 0 '変数
While i < 2
While j < 2
If (j = 0) Then
j = j + 1
Continue While
End If
Console.WriteLine("j=" & j) 'j=1が出力される
j = j + 1
End While
Console.WriteLine("i=" & i) 'i=0 i=1 が出力される
i = i + 1
End While
End Sub
End Module
6,7行目にWhile文があり入れ子になっています。
10行目のContinue Whileは、内側のWhile文の先頭の7行目に戻ります。
do...while文にあたる書き方
- do...while文にあたる書き方のサンプルです。
- 処理は、必ず1回は実行されます。
- 処理に、条件をfalseにするロジックがないと無限ループになるので注意が必要です。
Module Module1
Sub Main()
Dim a As Integer = 0 '変数
Dim b As Integer = 0 '変数
Do
Console.WriteLine(a) '0 1 2 3が出力される
a = a + 1
Loop While a < 3
While True
Console.WriteLine(b) '0 1 2 3が出力される
b = b + 1
If b > 2 Then
Exit While
End If
End While
End Sub
End Module
6~9行目はDo...Loop文でLoopの横にWhileと条件があります。最初の1回は必ず実行されます。
11行目は、While Trueで最初の1回は必ず実行されます。
13行目は、ループの変数を1加算しています。
14行目の条件がtrueになったときに15行目のExitでWhile文を抜けます。
比較演算子
上記コードの=は比較演算子です。
比較演算子は、2つの値を比べて真偽値(true/false)を返します。
条件を満たしていればtrue,そうでない場合はfalseです。
等しくないとする場合は、<>とします。
比較演算子 | 説明 |
---|---|
a = b | aとbの値は等しい |
a <> b | aとbの値は等しくない |
a > b | aはbより大きい |
a >= b | aはbより大きい または 等しい |
a < b | aはbより小さい |
a <= b | aはbより小さい または 等しい |
以下は、Microsoftの比較演算子 (Visual Basic)のリンクです。
https://docs.microsoft.com/ja-jp/dotnet/visual-basic/language-reference/operators/comparison-operators
関連の記事