`
KellyLin1115
  • 浏览: 4675 次
  • 性别: Icon_minigender_2
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Controlling Execution

 
阅读更多
Iteration
The comma operator
Comma operator (not the comma separator, which is used to separate definitions and method arguments) has only one use in Java: in the control expression of a for loop. In both the initialization and step portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially.
for(int i = 1, j = i + 10; i < 5; i++, j = i * 2) {
      System.out.println("i = " + i + " j = " + j);
}
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.

The infamous “goto”
A label is an identifier followed by a colon, like this:
label1:

The only place a label is useful in Java is right before an iteration statement.
And that means right before—it does no good to put any other statement between the label and the iteration. And the sole reason to put a label before an iteration is if you’re going to nest another iteration or a switch (which you’ll learn about shortly) inside it. That’s because the break and continue keywords will normally interrupt only the current loop, but when used with a label, they’ll interrupt the loops up to where the label exists:
label1:
outer-iteration {
      inner-iteration {
        //...
        break; // (1)
        //...
        continue; // (2)
        //...
        continue label1; // (3)
        //...
        break label1; // (4)
    }
}
In (1), the break breaks out of the inner iteration and you end up in the outer iteration. In (2), the continue moves back to the beginning of the inner iteration.
But in (3), the continue label1 breaks out of the inner iteration and the outer iteration, all the way back to label1. Then it does in fact continue the iteration, but starting at the outer iteration.
In (4), the break label1 also breaks all the way out to label1, but it does not reenter the iteration. It actually does break out of both iterations.

Note that break breaks out of the for loop, and that the increment expression doesn’t occur until the end of the pass through the for loop.

t’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.

switch
The switch statement selects from among pieces of code based on the value of an integral expression.
switch(integral-selector) {
      case integral-value1 : statement; break;
      case integral-value2 : statement; break;
      case integral-value3 : statement; break;
      case integral-value4 : statement; break;
      case integral-value5 : statement; break;
      // ...
      default: statement;
}

The switch compares the result of integral-selector to each integral-value. If it finds a match, the corresponding statement (a single statement or multiple statements; braces are not required) executes. If no match occurs, the default statement executes.

The break is optional. If it is missing, the code for the following case statements executes until a break is encountered.

It requires a selector that evaluates to an integral value, such as int or char. If you want to use, for example, a string or a floating point number as a selector, it won’t work in a switch statement.

分享到:
评论

相关推荐

    java编程思想教学材料——第四讲Controlling Execution

    在Java编程中,控制执行是程序流程的基础,主要包括条件语句和循环语句。本讲主要探讨了如何使用这些结构来实现程序的逻辑控制。 首先,条件语句以`if`关键字开始,它允许我们根据特定条件执行代码块。...

    C程序设计教学课件:CHAPTER3FROMCTOC.pptx

    4. 控制执行(Controlling Execution) 控制执行包括流程控制语句,如条件语句(if,else,switch)和循环语句(while,for,do-while)。这些语句允许程序员根据条件执行不同的代码块,或者重复执行某段代码直到...

    Spring Batch in Action

    - **Controlling execution**:介绍了如何控制作业的执行流程,包括跳过策略和终止条件。 - **Enterprise integration**:讨论了Spring Batch如何与其他企业级应用集成,如JMS、Web Services等。 - **Monitoring ...

    东北大学c++课程PPT 第一章

    1. 控制执行(Controlling Execution) - while循环:只要控制表达式为真,就重复执行语句。 - do-while循环:至少执行一次,即使控制表达式在第一次时为假。 - for循环:在初始化、条件检查和步进表达式中提供更...

    我的c++习题

    ### 控制执行(Controlling Execution) 控制结构允许程序根据不同的条件来决定代码的执行路径。常见的控制结构包括条件语句(如`if`, `if-else`)和循环语句(如`for`, `while`)。例如,在题目中出现的`while(x )...

    thinking in Java guide solutio

    Controlling Execution (控制执行流程) - **条件语句**:if-else、switch-case结构用于基于条件做出不同的决策。 - **循环语句**:for、while、do-while循环用于重复执行某段代码直到满足某个条件为止。 - **跳转...

    Professional Assembly Language

    Chapter 6, “Controlling Execution Flow,” describes the branching instructions used in assembly lan- guage programs. Possibly one of the most important features of programs, the ability to recognize ...

    Windows Runtime via C#

    deploy, and secure app packages Understand how apps are activated and the process model controlling their execution Study the rich features available when working with files and folders Explore how ...

    OS X and iOS Kernel Programming

    device driver for controlling internal or external hardware devices. Because of this, much of the focus of this book is centred on the development of device drivers. The primary framework for device ...

    Biblio.Distribution.C++.For.Artists.The.Art.Philosophy.and.Science.of.Object-Oriented.Programming.2003.chm

    Chapter 6 - Controlling The Flow Of Program Execution Chapter 7 - Pointers and References Chapter 8 - Arrays Chapter 9 - Functions Chapter 10 - Toward Problem Abstraction—Creating New Data ...

    操作系统第一、二章复习题答案精编版.doc

    操作系统是计算机系统的核心组成部分, plays a crucial role in controlling program execution and managing computer system resources. Its development and application have a significant impact on the ...

    SAP 配置指南(详细)英文版.pdf

    - 物流执行(Logistics Execution) - 定义、复制、删除、检查仓库号码(Define, copy, delete, check warehouse number):创建和管理用于库存管理的仓库号码。 - 定义、复制、删除、检查发货点(Define, copy, ...

    MCTS Self-Paced Training Kit (Exam 70-448)

    - **Managing SSIS Package Execution (Chapter 4, Lesson 2):** Controlling the execution of SSIS packages through scheduling, parameters, and other mechanisms. - **Configuring SSIS Security Settings ...

    CUDA11.0-C-Programming-Guide.pdf

    This feature allows developers to optimize memory access patterns and improve performance by controlling how data is cached. 3. **Asynchronous Copy Data from Global to Shared Memory** - Developers ...

    Mastering.Ansible.178439548X

    Explore use cases for Ansible's advanced features including task delegation, fast failures, and serial task execution Extend Ansible with custom modules, plugins, and inventory sources Who This Book ...

Global site tag (gtag.js) - Google Analytics