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?

Javaで「for文を使った繰り返し処理」の動作を確認してみた

Posted at

概要

Javaで「for文を使った繰り返し処理」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample2_1.java
class JSample2_1{
  public static void main(String[] args){
    int sum = 0;

    for (int i = 1; i <= 5; i++){
      System.out.println(i);
      sum += i;
    }

    System.out.println("合計=" + sum);
  }
}
JSample2_2.java
class JSample2_2{
  public static void main(String[] args){
    int i;
    double d;

    for (i = 1, d = 2.4; i <= 5; i++, d += 0.1){
      System.out.println("i = " + i + ", d = " + d);
    }
  }
}
JSample2_3.java
class JSample2_3{
  public static void main(String[] args){
    for (int i = 1; i < 3; i++){
      for (int j = 1; j < 3; j++){
        for (int k = 1; k < 3; k++){
          System.out.println("i = " + i + ", j = " + j + ", k = " + k);
        }
      }
    }
  }
}

以下のコマンドを実行しました。

$ javac JSample2_1.java 
$ java JSample2_1 
1
2
3
4
5
合計=15
$ javac JSample2_2.java 
$ java JSample2_2 
i = 1, d = 2.4
i = 2, d = 2.5
i = 3, d = 2.6
i = 4, d = 2.7
i = 5, d = 2.8000000000000003
$ javac JSample2_3.java 
$ java JSample2_3 
i = 1, j = 1, k = 1
i = 1, j = 1, k = 2
i = 1, j = 2, k = 1
i = 1, j = 2, k = 2
i = 2, j = 1, k = 1
i = 2, j = 1, k = 2
i = 2, j = 2, k = 1
i = 2, j = 2, k = 2

まとめ

何かの役に立てばと。

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?