Javaのwhileループ は、指定されたブール条件に基づいてコードを繰り返し実行できるようにする制御フロー ステートメントです。 while ループは、繰り返しの if ステートメントと考えることができます。 Java の While ループは、ステートメントのブロックを繰り返し実行する必要がある場合に使用されます。 while ループは、繰り返しの if ステートメントとみなされます。反復回数が固定されていない場合は、while ループを使用することをお勧めします。

構文:
while (test_expression) { // statements update_expression; }> 注記: while(condition ) の後に中括弧「{」と「}」を指定しない場合、デフォルトで while ステートメントは、直前の 1 つのステートメントがそのブロック内にあるとみなします。
while (テスト式)
// while 内の単一ステートメントのみ
Java While ループの一部
さまざまな While ループの一部 は:
1. テスト式: この式では、条件をテストする必要があります。条件が true と評価された場合、ループの本体が実行され、式の更新に進みます。それ以外の場合は、while ループから抜けます。
Javaの個別の文字列
例:
i <= 10>
2. 式の更新 : ループ本体の実行後、この式はループ変数をある値ずつ増加/減少させます。
例:
i++;>
While ループはどのように実行されるのでしょうか?
- 制御は while ループに入ります。
- フローは条件にジャンプします
- 状態はテスト済みです。
- Condition が true の場合、フローは Body に入ります。
- Condition が false を返した場合、フローはループの外に出ます。
- ループ本体内のステートメントが実行されます。
- 更新が行われます。
- 制御はステップ 2 に戻ります。
- while ループが終了し、フローが外側に出ました。
while ループ (制御フロー) のフローチャート:

Javaのwhileループの例
例 1: このプログラムは、Hello World を 5 回出力しようとします。
私のコンピュータの画面はどれくらいの大きさですか
ジャワ
// Java program to illustrate while loop.> class> whileLoopDemo {> >public> static> void> main(String args[])> >{> >// initialization expression> >int> i =>1>;> >// test expression> >while> (i <>6>) {> >System.out.println(>'Hello World'>);> >// update expression> >i++;> >}> >}> }> |
>
Javaハッシュマップとは何ですか
>出力
Hello World Hello World Hello World Hello World Hello World>
上記のメソッドの複雑さ:
時間計算量: ○(1)
補助スペース: ○(1)
ドライランニングの例 1: プログラムは次のように実行されます。
1. Program starts. 2. i is initialized with value 1. 3. Condition is checked. 1 <6 yields true. 3.a) 'Hello World' gets printed 1st time. 3.b) Updation is done. Now i = 2. 4. Condition is checked. 2 <6 yields true. 4.a) 'Hello World' gets printed 2nd time. 4.b) Updation is done. Now i = 3. 5. Condition is checked. 3 <6 yields true. 5.a) 'Hello World' gets printed 3rd time 5.b) Updation is done. Now i = 4. 6. Condition is checked. 4 <6 yields true. 6.a) 'Hello World' gets printed 4th time 6.b) Updation is done. Now i = 5. 7. Condition is checked. 5 <6 yields true. 7.a) 'Hello World' gets printed 5th time 7.b) Updation is done. Now i = 6. 8. Condition is checked. 6 <6 yields false. 9. Flow goes outside the loop. Program terminates.>
例 2: このプログラムは 1 から 10 までの数値の合計を求めます。
ジャワ
Cプログラムの文字列配列
// Java program to illustrate while loop> class> whileLoopDemo {> >public> static> void> main(String args[])> >{> >int> x =>1>, sum =>0>;> >// Exit when x becomes greater than 4> >while> (x <=>10>) {> >// summing up x> >sum = sum + x;> >// Increment the value of x for> >// next iteration> >x++;> >}> >System.out.println(>'Summation: '> + sum);> >}> }> |
>
Javaでの文字列配列の作成
>出力
Summation: 55>
上記の方法の複雑さ
時間計算量: ○(1)
補助スペース: ○(1)
Java while Loop のビデオ参照
関連記事:
- Javaのループ
- Java For ループと例
- Java do-while ループと例
- C、C++、Java における for ループと while ループの違い
- C、C++、Java における while ループと do-while ループの違い