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. MFC 打开文件对话框 打开单个文件

    CFileDialog的语法: CFileDialog(BOOL bOpenFileDialog,LPCTSTR lpszDefExt=NULL,LPCTSTR lpszFileName=NULL,D ...

  2. Redis入门指南

    随着互联网业务对性能需求日益强烈,作为Key/Value存储的Redis具有数据类型丰富和性能表现优异的特点.如果能够熟练地驾驭它,不管是把它用做缓存还是存储,对很多大型应用都很多帮助.新浪作为世界上 ...

  3. DependencyProperties or INotifyPropertyChanged ?

    When you want to make an object binding-aware you have two choices : implements INotifyPropertyChang ...

  4. Mariadb 数据库写入中文乱码问题

    从其他表里面导入数据,出现中文乱码错误.之前操作时并没有碰到类似问题,有些不得其解. 在网上搜了下,最后参考这篇文章,在执行insert前,先执行 set names gbk; 然后成功插入. 链接: ...

  5. BZOJ 2427 & 分块裸题

    题意: 求区间内的众数,强制在线. SOL: 推荐一个大神犇的blog,讲的还是很好的(主要我喜欢他的代码风格(逃:http://www.cnblogs.com/JoeFan/p/4248767.ht ...

  6. ACM 一种排序

    一种排序 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编号.长.宽都是整数 ...

  7. [深入浅出Windows 10]实现饼图控件

    13.2 实现饼图控件 上一小节讲解了动态生成折线图和区域图,对于简单的图形这样通过C#代码来生成的方式是很方便的,但是当我们的图表要实现更加复杂的逻辑的时候,这种动态生成的方式就显得力不从心了,那就 ...

  8. (转)FTP操作类,从FTP下载文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net ...

  9. Centos 安装 NodeJS

    准备命令: yum -y install gcc make gcc-c++ openssl-devel wget 下载源码及解压: wget http://nodejs.org/dist/v0.10. ...

  10. [LintCode] Paint House 粉刷房子

    There are a row of n houses, each house can be painted with one of the three colors: red, blue or gr ...