--Sequentially
--Branch
- if
, switch
--Repeat
- while
,for
if
switch
//if syntax
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 syntax
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 syntax
int w=1;
while(w<4){
System.out.println("Count No."+w+" by 'while'.");
w++;
}
//do while syntax
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 syntax
for(int i=1; i<=3; i++){
System.out.println("Count No."+i+" by 'for'.");
}
-- continue
: Continue the loop.
--break
: Break out of the loop.
//Repeated interruption
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)'.");
}
[Introduction to Java 2nd Edition] (https://www.amazon.co.jp/dp/B00MIM1KFC/ref=dp-kindle-redirect?_encoding=UTF8&btkr=1) Pp.098-133.
Recommended Posts