Thinking in Java——笔记(4)

Controlling Execution


true and false

  • Java doesn’t allow you to use a number as a boolean.
  • If you want to use a non-boolean in a boolean test, you must first convert it to a boolean value by using a conditional expression.

if-else

Iteration

  • Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements.

do-while

for

  • The expression is tested before each iteration, and as soon as it evaluates to false, execution will continue at the line following the for statement. At the end of each loop, the step executes.
  • In Java and C++, you can spread your variable declarations throughout the block, defining them at the point that you need them.

The comma operator

  • Using the comma operator, you can define multiple variables within a for statement, but they must be of the same type.
  • The ability to define variables in a control expression is limited to the for loop.

Foreach syntax

  • Any method that returns an array is a candidate for use with foreach.
  • Foreach will also work with any object that is Iterable.
  • it is far easier to read and says what you are trying to do rather than giving the details of how you are doing it.

Return

break and continue

  • break quits the loop without executing the rest of the statements in the loop.
  • continue stops the execution of the current iteration and goes back to the beginning of the loop to begin the next iteration.
  • Normally, you’d use a break like this only if you didn’t know when the terminating condition was going to occur.

The infamous “goto”

  • A goto is a jump at the source-code level.
  • The problem is not the use of goto, but the overuse of goto.
  • Although goto is a reserved word in Java, it is not used in the language; Java has no goto.
  • It’s important to remember that the only reason to use labels in Java is when you have nested loops and you want to break or continue through more than one nested level.
  • The break and continue keywords will normally interrupt only the current loop.
  • In the cases where breaking out of a loop will also exit the method, you can simply use a return.

switch

  • The switch statement selects from among pieces of code based on the value of an integral expression.
  • If it finds a match, the corresponding statement executes. If no match occurs, the default statement executes.
  • If break is missing, the code for the following case statements executes until a break is encountered.
  • Note that the last statement, following the default, doesn’t have a break because the execution just falls through to where the break would have taken it anyway.
  • It requires a selector that evaluates to an integral value, such as int or char.
  • enums are designed to work nicely with switch.
  • Notice how the cases can be “stacked” on top of each other to provide multiple matches for a particular piece of code.
posted @ 2016-11-29 15:13  玄天强  阅读(268)  评论(0编辑  收藏  举报