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. UITextField最大字符数和最大字节数的限制

    UITextView,UITextfield中有很多坑,网上的方法也很多,但是用过之后暂时没有发现一个好用.这里我给大家几组测试用例可以一试,为啥不好用. 限制10个字节,输入2个Emoj之后是8个字 ...

  2. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  3. HDU5863 cjj's string game(DP + 矩阵快速幂)

    题目 Source http://acm.split.hdu.edu.cn/showproblem.php?pid=5863 Description cjj has k kinds of charac ...

  4. Excel: Switch (transpose) columns and rows

    链接:https://support.office.com/en-in/article/Switch-transpose-columns-and-rows-ed1215f5-59af-47e6-953 ...

  5. BZOJ3514 : Codechef MARCH14 GERALD07加强版

    以边编号为权值 用Link-cut Tree维护最大生成树 对于新加的第i条边(u,v) a[i]表示当a[i]这条边加入后连通块个数会减少 若u==v则a[i]=m 若u与v不连通则连上,a[i]= ...

  6. 【Poj】 p2342 Anniversary party(树形DP第一道)

    Anniversary party Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5523   Accepted: 3169 ...

  7. reason: '*** Collection <__NSCFArray: 0x7ffa43528f70> was mutated while being enumerated.'

    一,错误分析 1.崩溃代码如下: //遍历当前数组,判断是否有相同的元素 for (NSString *str in self.searchHistoryArrM) { if ([str isEqua ...

  8. Android studio 签名使用转

    来自http://www.cnblogs.com/xiwix/archive/2012/04/15/2447910.html 本文主要讲解Android应用程序签名相关的理论知识,包括:什么是签名.为 ...

  9. html下拉菜单的实现

    这是简单的下拉菜单 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...

  10. (转)C#特性学习与使用(为枚举定义Description)

    参考:http://blog.csdn.net/nndtdx/article/details/6905802#comments