Help us understand the problem. What is going on with this article?

【Java】制御構文ノート

More than 1 year has passed since last update.

制御構文

構造化定理

  • 順次
  • 分岐
    • if, switch
  • 繰り返し
    • while,for

分岐

  • if
  • switch
// if構文
int r=new java.util.Random().nextInt(2);
if (r<1){
    System.out.println("This is true by 'if'.");
}else{
    System.out.println("This is false by 'if'.");
}

// switch構文
int s=new java.util.Random().nextInt(3);
switch(s){
    case 0:
        System.out.println("This is Case.0 by 'switch'.");
        break;
    case 1:
        System.out.println("This is Case.1 by 'switch'.");
        break;
    case 2:
        System.out.println("This is Case.2 by 'switch'.");
        break;
    default:
        System.out.println("This is default by 'switch'.");
}

繰り返し

  • while
  • do while
  • for
// while構文
int w=1;
while(w<4){
    System.out.println("Count No."+w+" by 'while'.");
    w++;
}

// do while構文
int dw=3;
do{
    System.out.println("Count No."+dw+" by 'do while'.");
    dw--;
    System.out.println("Count decrease -1 by 'do while'.");
}while(dw>0);


// for構文
for(int i=1; i<=3; i++){
    System.out.println("Count No."+i+" by 'for'.");
}

「繰り返しの中断」という概念

  • continue:ループを続ける。
  • break:ループを抜ける。
// 繰り返しの中断
for(int i=5; i>=1; i--){
    if(i==3){
        continue;
    }
    if(i==2){
        break;
    }
    System.out.println("Count No."+i+" by 'for/continue(3)/break(2)'.");
}

参考書籍

スッキリわかるJava入門第2版
Pp.098-133.

shg3
学習
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away