制御構文#
構造化定理##
- 順次
- 分岐
-
if,switch
-
- 繰り返し
-
while,for
-
分岐##
ifswitch
// 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'.");
}
繰り返し##
whiledo whilefor
// 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版]
(https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1)
Pp.098-133.