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. 在 Chrome 中调试 Android 浏览器

    最近需要使用 Chrome Developer Tools 调试 Android 浏览器,但是官方指南并不是很好使,经过一番折腾,终于调试成功了,在此把经验分享给需要的朋友. Chrome Devel ...

  2. sprint3冲刺第二天

    队友: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  3. Ubuntu Mono 运行 Helloworld

    Ubuntu Mono 运行 Helloworld ref: http://www.mono-project.com/docs/getting-started/install/linux/#usage ...

  4. three.js正交投影照相机

    照相机又分为正交投影照相机与透视投影照相机 举个简单的例子来说明正交投影与透视投影照相机的区别.使用透视投影照相机获得的结果是类似人眼在真实世界中看到的有"近大远小"的效果(如下图 ...

  5. <构建之法>第十一章、十二章有感

    十一章:软件设计与实现 工作时要懂得平衡进度和质量.我一直有一个困扰:像我们团队这次做 男神女神配 社区交友网,我负责主页的设计及内容模块,有个队友负责网站的注册和登录模块,有个队友负责搜索模块,有个 ...

  6. Leetcode Add Binary

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

  7. ACM: 强化训练-Inversion Sequence-线段树 or STL·vector

    Inversion Sequence Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%lld & %llu D ...

  8. [BZOJ2794][Poi2012]Cloakroom

    2794: [Poi2012]Cloakroom Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 167  Solved: 119[Submit][St ...

  9. 【CodeVS】P1041 car的旅行路线

    题目描述 Description 又到暑假了,住在城市A的Car想和朋友一起去城市B旅游.她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一条笔直的高速铁路,第I ...

  10. 使用command对象操作数据库

    1.Command对象查询数据库 protected void Button1_Click(object sender, EventArgs e) { //读取web.config节点配置 strin ...