The java program flow control

Sequential structure : Code executed from top to bottom;

Branch structure :

if () {

} else{

}

if () {

} else if () {

} else {

}

switch (constant) {

case constants:

  Statements;

  break;

case constants:

  Statements;

  break;

default:

  Statements;

  break;

}

swich rules :

  • switch in expression of the return value must be the following types: byte short char int enumeration String
  • Case clause value must be a constant, and all values ​​in the case clause should be different
  • optionally default statement is optional, when no matching CASET, performs default
  • break statement blocks for terminating switch statement been executed a branch, if not break, will be executed in sequence to the end

When to use if and switch it?

  • If it is determined the specific values ​​is small, and in line with byte short int char four types, although can be used, but it is recommended to use switch, more efficient;
  • Other cases, the determination of the interval, the result of the boolean type determination, if used, if the use of a wider range;

Loop structure :

for (int i=0;i<100;i++) {}

while (logic operations, for example: i <100) {

  i++

}

do {

  i++

} while(i<100)

Nested loop structure;

break, continue and return to use;

Guess you like

Origin www.cnblogs.com/xiximayou/p/12039000.html