Javaのループ ステートメントのブロックを繰り返し実行する必要がある場合に使用されます。ジャワ do-while ループ です 制御ループを終了する 。したがって、とは異なり、 のために または その間 ループ、ループ本体のステートメントを実行した後の条件の do-while チェック。
構文:
do { // Loop Body Update_expression } // Condition check while (test_expression);> 注記: の テスト式 do-while ループは、 ブール値 value を指定しないと、コンパイル時エラーが発生します。
do-while の適用: そのサンプル アプリケーションは、ユーザーに何らかのメニューを表示しています。
例えば:
ユーザーにいくつかのオプションを表示し、1 を押してこれを実行する、2 を押してこれを実行するなどのオプションを表示し、「Q」を押してゲームを終了するゲームを実装しています。ここでは、ゲーム メニューを少なくとも 1 回はユーザーに表示する必要があるため、do-while ループ内にゲーム メニューのコードを記述します。

図:
ジャワ
whileループJavaを実行します
// Java Program to Illustrate One Time Iteration> // Inside do-while Loop> // When Condition IS Not Satisfied> // Class> class> GFG {> >// Main driver method> >public> static> void> main(String[] args)> >{> >// initial counter variable> >int> i =>0>;> >do> {> >// Body of loop that will execute minimum> >// 1 time for sure no matter what> >System.out.println(>'Print statement'>);> >i++;> >}> >// Checking condition> >// Note: It is being checked after> >// minimum 1 iteration> >while> (i <>0>);> >}> }> |
BFSとDFS
>
>出力
Print statement>
出力の説明:
上記のコードでは、後で条件がチェックされるため、 do 内の本体が必ず 1 回実行されるため、条件が後でチェックされることがわかりました。したがって、メニューを表示し、後でターミナル上でコマンドを続行したい場合は、常に do-while ループを使用します。
do-while ループのコンポーネント
A. テスト式: この式では、条件をテストする必要があります。条件が true と評価された場合、ループの本体が実行され、式の更新に進みます。それ以外の場合は、while ループから抜けます。例えば:
i <= 10>
B. 式の更新 : ループ本体の実行後、この式はループ変数をある値ずつ増加/減少させます。例えば:
i++;>
do-whileループの実行
- 制御は do-while ループに入ります。
- ループ本体内のステートメントが実行されます。
- 更新が行われます。
- フローは条件にジャンプします
- 状態はテスト済みです。
- 条件が true の場合は、ステップ 6 に進みます。
- Condition が false を返した場合、フローはループの外に出ます。
- フローはステップ 2 に戻ります。
フローチャートの do-while ループ:

実装:
例 1: このプログラムは、Hello World を 5 回出力しようとします。
ジャワ
// Java Program to Illustrate Do-while Loop> // Class> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >// Declaring and initialization expression> >int> i =>1>;> >// Do-while loop> >do> {> >// Body of do-while loop> >// Print statement> >System.out.println(>'Hello World'>);> >// Update expression> >i++;> >}> >// Test expression> >while> (i <>6>);> >}> }> |
Javaラムダ
>
>出力:
Hello World Hello World Hello World Hello World Hello World>
補助スペース: O(1)
出力の説明:
プログラムは次のように実行されます。
- プログラムが始まります。
- i は値 1 で初期化されます。
- 実行がループに入る
- Hello World が最初に出力されます。
- アップデートが完了しました。これで i = 2 になります。
- 状態確認済みです。 2 <6 は true を返します。
- 実行はループに入ります。
- Hello World は 2 回目に出力されます。
- アップデートが完了しました。これで i = 3 になります。
- 状態確認済みです。 3 <6 は true を返します。
- 実行がループに入る
- Hello World が 3 回目に印刷される
- アップデートが完了しました。これで i = 4 になります。
- 状態確認済みです。 4 <6 は true を返します。
- 実行がループに入る
- Hello World が 4 回目に印刷される
- アップデートが完了しました。これで i = 5 になります。
- 状態確認済みです。 5 <6 は true を返します。
- 実行がループに入る
- Hello World が 5 回目に印刷される
- アップデートが完了しました。これで i = 6 になります。
- 状態確認済みです。 6 <6 は false を返します。
- フローはループの外に出ます。
例 2
ジャワ
// Java Program to Illustrate Do-while Loop> // Class> class> GFG {> >// Main driver method> >public> static> void> main(String args[])> >{> >// Declaring and initializing integer values> >int> x =>21>, sum =>0>;> >// Do-while loop> >do> {> >// Execution statements(Body of loop)> >// Here, the line will be printed even> >// if the condition is false> >sum += x;> >x--;> >}> >// Now checking condition> >while> (x>>>10>);> >// Summing up> >System.out.println(>'Summation: '> + sum);> >}> }> |
Arduinoの機能
>
>出力:
Summation: 176>
例 3: 中括弧 {} を使用しない do-while ループ
ブール値を文字列に変換する
ジャワ
/*package whatever //do not write package name here */> import> java.io.*;> class> GFG {> >public> static> void> main (String[] args) {> >int> i=>1>;> >do> >// only single statement in do block> >System.out.println(>'Hello GFG!'>);> >// this condition is false so only do block will execute> >while>(i>=>3>);> > > >}> }> |
>
>出力
Hello GFG!>
&list=PLqM7alHXFySF5ErEHA1BXgibGg7uqmA4_&ab_channel=techcodeview.com
関連記事:
- Javaのループ
- Java For ループと例
- Java while ループと例
- C、C++、Java における while ループと do-while ループの違い
- C、C++、Java における for ループと do-while ループの違い