0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Java基礎学習内容4(繰り返し)

Posted at

while(do while)

条件式がtrueの場合処理を繰り返す。

while
// while
while(flag){
  // 処理
}

// do while
do {
  // 処理
} while(flag);

for

初期化、繰り返し条件、カウンタ変数の更新を設定して繰り返し処理を行う。

for
for(int counter = 0;counter < 5;counter++){
  // 処理
}

また、Collectionの各要素に対して繰り返し処理を行う拡張for文もある。

拡張for
for(String str : strList){
  // 処理
}

break

breakを用いることで繰り返し処理から抜けることが出来る。
また、ラベルを併用することで入れ子の繰り返し処理から抜けることも可能。

break
while(true){
  ...
  break;
}

label:
while(true){
  while(true){
    ...
    break label;
  }
}

continue

continueを用いることで繰り返し処理内の後続処理をスキップし、次の要素から処理を行うことが出来る。

continue
while(true){
  ...
  continue;
  ... // 後続処理がスキップされる

}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?