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.

Thinking in Java——笔记(4)的更多相关文章

  1. Effective Java笔记一 创建和销毁对象

    Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...

  2. java笔记00-目录

    --2013年7月26日17:49:59 学习java已久,趁最近有空,写一个总结: java笔记01-反射:

  3. java笔记整理

    Java 笔记整理 包含内容     Unix Java 基础, 数据库(Oracle jdbc Hibernate pl/sql), web, JSP, Struts, Ajax Spring, E ...

  4. 转 Java笔记:Java内存模型

    Java笔记:Java内存模型 2014.04.09 | Comments 1. 基本概念 <深入理解Java内存模型>详细讲解了java的内存模型,这里对其中的一些基本概念做个简单的笔记 ...

  5. servlet(6) - servlet总结 - 小易Java笔记

    垂阅前必看: 这都是我总结的我觉得是学习servlet应该掌握的,我在学习期间也做了一个博客项目来让所学的知识得以巩固.下面就是博客项目链接.前面的servlet相关的笔记总汇,还有就是我把觉得在学习 ...

  6. Java笔记 —— 继承

    Java笔记 -- 继承 h2{ color: #4ABCDE; } a{ text-decoration: none!important; } a:hover{ color: red !import ...

  7. Java笔记 —— 方法重载和方法重写

    Java笔记 -- 方法重载和方法重写 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red ...

  8. Java笔记 —— 初始化

    Java笔记 -- 初始化 h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: red !impo ...

  9. Java笔记 —— this 关键字

    Java笔记 -- this 关键字 h2{ color: #4ABCDE; } a{ color: blue; text-decoration: none; } a:hover{ color: re ...

  10. Java 笔记 —— java 和 javac

    Java 笔记 -- java 和 javac h2{ color: #4ABCDE; } a{ text-decoration: none !important; } a:hover{ color: ...

随机推荐

  1. 并查集(加权) LA 4487 Exclusive-OR

    题目传送门 题意:训练指南P245 分析:首先这道是经典的并查集题目,利用异或的性质.异或性质:x ^ 0 = x -> a ^ a = 0 -> x ^ a ^ a = x,即一个数对某 ...

  2. Tomcat在局域网中localhost可以访问,但是无法通过本地ip访问,127.0.0.1也无法访问问题的解决方法

    环境:Tomcat6,Windows Server2008 R2, Tomcat使用默认端口8080. 在BO服务器上使用Tomcat6作为WEB服务器,在服务器本地使用http://localhos ...

  3. Modify a Stored Procedure using SQL Server Management Studio

    In Object Explorer, connect to an instance of Database Engine and then expand that instance. Expand  ...

  4. 据说最近IMO中国队失利的一题

    (图基于Microsoft PaintBrush技术构建) 平面几何是可以难得出蛆的.这道题难在多圆.高度非对称和具有一定复杂性.如图,对ABC,H是垂心,O是垂足,M是中点.QK在ABC外接圆上,均 ...

  5. ios编译ASIHTTPRequest时出现 'libxml/HTMLparser.h' file not found in ASIHTTPRequest

    解决方法是添加libxml2.dylib 然后在Build Settings 中的 Header Search Paths 添加: ${SDK_DIR}/usr/include/libxml2

  6. ACM: HDU 5285 wyh2000 and pupil-二分图判定

     HDU 5285  wyh2000 and pupil Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%I64d &a ...

  7. NOIp 2012 #1 Vigenère 密码 Label:模拟

    题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南 ...

  8. chrome快捷键,让开发更快捷:

    9:18 2015/12/9chrome快捷键,让开发更快捷:部分:按住 Ctrl 键,然后点击链接 从后台在新标签页中打开链接,但您仍停留在当 前标签页中 按住 Ctrl+Shift 键,然后点击链 ...

  9. 【BZOJ2186】【SDoi2008】沙拉公主的困惑 数论

    Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁国现 ...

  10. NOI模拟赛 Day1

    [考完试不想说话系列] 他们都会做呢QAQ 我毛线也不会呢QAQ 悲伤ING 考试问题: 1.感觉不是很清醒,有点困╯﹏╰ 2.为啥总不按照计划来!!! 3.脑洞在哪里 4.把模拟赛当作真正的比赛,紧 ...