[Java Review] 2: Java syntax and more

The following notes are based on the book: << Head First JAVA >> and video series: 手把手教你用Java基础教程 - 北京尚学堂 - 马士兵

- For loop

The process follows:

  1. execute ep1
  2. if ep2 is true, go to 3; otherwise break;
  3. execute statements
  4. execute ep3
  5. iterate again 2~4
1
2
3
4
for (ep1; ep2; ep3){
statement1;
statement2;
}
- While loop
1
2
3
4
while(ep){
statement1;
statement2;
}
- Do while loop
1
2
3
4
5
//noteic the ';' after while!!!
do {
st1;
st2;
} while (ep);
- If else
1
2
3
4
5
if (ep1){}

if (ep1){} else{}

if (ep1){} else if (ep2){} else{}
- Switch
  • i must be int (char, short).
  • if i = 8 , st1 is executed.
  • case can dig through if breaknot existed. ex. if i = 10, st3 and st4 are executed.
  • default is used to handle no-match.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//example
int i = 8;
switch (i) {
case 8:
st1;
break;
case 10:
case 11:
st3;
case 12:
st4;
break;
default:
st5;
}
0%