-
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.
-
相关阅读:
JS-得到屏幕宽高、页面宽高
CSS3-border-radius 属性
从30岁到35岁:为你的生命多积累一些厚度【转载】
HTML5-IOS WEB APP应用程序(IOS META)
HTML-Meta中的viewport指令
EasyUI-window包含一个iframe,在iframe中如何关闭window
JS-为句柄添加监听函数
EasyUI-EasyUI框架入门学习
Linux下的C编程
***经典笔试题
-
原文地址:https://www.cnblogs.com/apolloqq/p/6113724.html
Copyright © 2020-2023
润新知