`
leonzhx
  • 浏览: 770651 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 4. Controlling Execution -- Thinking in Java

阅读更多

1) Java doesn’t allow you to use a number as a boolean.

 

2) The if-else statement is the most basic way to control program flow. The else is optional, so you can use if in two forms:

if(Boolean-expression)
    statement;

 

or

if(Boolean-expression)
    statement;
else
    statement;

 

The statement is either a simple statement terminated by a semicolon, or a compound statement, which is a group of simple statements enclosed in braces.

 

3) Looping is controlled by while, do-while and for, which are sometimes classified as iteration statements. A statement repeats until the controlling boolean-expression evaluates to false. The form for a while loop is:

while(boolean-expression)
    statement; 

 The boolean-expression is evaluated once at the beginning of the loop and again before each further iteration of the statement.

 

4) The static method random( ) in the Math library generates a double value between 0 and 1. (It includes 0, but not 1.)

 

5) The form for do-while is

do
    statement;
while(boolean-expression);

 The sole difference between while and do-while is that the statement of the do-while always executes at least once, even if the expression evaluates to false the first time.

 

6) for loop performs initialization before the first iteration. Then it performs conditional testing and, at the end of each iteration, some form of “stepping.” The form of the for loop is:

for(initialization; boolean-expression; step)
    statement;

 
Any of the expressions: initialization, boolean-expression or step can be empty. The expression is tested before each iteration, and as soon as it evaluates to true, execution will continue at the line following the for statement. At the end of each loop, the step executes.

 

7) 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 (not limited in for loop) and step (limited in for loop) portions of the control expression, you can have a number of statements separated by commas, and those statements will be evaluated sequentially. Using the comma operator, you can define multiple variables within a for statement, but they must be of the same type.

 

example : int i=1, j=2;

 

8) Java SE5 introduces a new and more succinct for syntax, for use with arrays and containers. This is often called the foreach syntax, and it means that you don’t have to create an int to count through a sequence of items—the foreach produces each item for you. The general form of it is :

for ( iterating variable definition: Array or Iterable Objects )
    statement;

  

9) Overloading means the same method name can be used with different argument lists.

 

10) Several keywords represent unconditional branching, which simply means that the branch happens without any test. These include return, break, continue, and a way to jump to a labeled statement which is similar to the goto in other languages.

 

11) The return keyword has two purposes: It specifies what value a method will return (if it doesn’t have a void return value) and it causes the current method to exit, returning that value. If you do not have a return statement in a method that returns void, there’s an implicit return at the end of that method, so it’s not always necessary to include a return statement. However, if your method states it will return anything other than void, you must ensure every code path will return a value.

 

12) You can also control the flow of the loop inside the body of any of the iteration statements by using 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.

 

13) 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 inside it and you want to break or continue through more than one nested level. 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.

 

14) The switch is sometimes called a selection statement. The switch statement selects from among pieces of code based on the value of an integral expression. Its general form is:

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;
}

 Integral-selector is an expression that produces an integral value. 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. The switch statement is a clean way to implement multiway selection, but 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. (JDK 7 supports string selector)

分享到:
评论

相关推荐

    The.Cucumber.for.Java.Book.Behaviour-Driven.Development

    Chapter 4. Step Definitions: From the Outside Chapter 5. Expressive Scenarios Chapter 6. Keeping Your Cucumbers Sweet Part II A Worked Example Chapter 7. Step Definitions: On the Inside Chapter 8. ...

    RemObjects DataAbstract Documents mht

    DA05 - Advanced options for controlling... DA06 - Strongly Typed DataTables and... DA08 - No-Code n-Tier Applications DA09 - Why Data Abstract? DA10 - The Business Processor in Depth DA11 - Business ...

    Learning.Puppet.4.1491907

    Chapter 4. Writing Manifests Chapter 5. Using the Puppet Configuration Language Chapter 6. Controlling Resource Processing Chapter 7. Expressing Relationships Chapter 8. Upgrading Puppet 3 Manifests ...

    Thinking in Java 4th Edition

    Controlling Execution 93 true and false..................... 93 if-else .................................. 93 Iteration ............................... 94 do-while .......................................

    SAS_Programming_III英文

    4.6 Solutions to Exercises..................................................................................................4-119 Chapter 5 Combining Data Vertically .....................................

    Internet of Things Programming with JavaScript

    Chapter 4. Control-Connected Devices Chapter 5. Adding a Webcam to Monitor Your Security System Chapter 6. Building a Web Monitor and Controlling Devices from a Dashboard Chapter 7. Building a Spy ...

    Learning.Puppet.4.1491907665.Early.Release

    Chapter 4. Writing Manifests Chapter 5. Using Puppet Configuration Language Chapter 6. Controlling Resource Processing Chapter 7. Expressing Relationships Chapter 8. Upgrading Puppet 3 Manifests ...

    Thinking in C: Foundations for Java & C++

    Thinking in C: Foundations for Java & C++ by Chuck Allison produced by Bruce Eckel Chapter 1: Introduction and Getting Started40 MinutesStart Lecture Chapter 2: Fundamental Data Types41 ...

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

    Learning.Cocos2d-JS.Game.Development

    Chapter 4: Learn about Swipes through the Making of Sokoban Chapter 5: Become a Musical Maestro Chapter 6: Controlling the Game with Virtual Pads Chapter 7: Adding Physics to Your Games Using Box2D ...

    Thinking In Java 4th Edition.pdf

    Controlling Execution 93  Initialization & Cleanup 107  Access Control 145  Reusing Classes 165  Polymorphism 193  Interfaces 219  Inner Classes 243  Holding Your Objects 275  Error Handling ...

    ur5_controller-master.zip

    Lightweight python library for controlling UR5 robot from Universal Robot family. Robot kinematics are calculated based on the following paper: [tech report]...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: Delphi/CBuilder XE4 now supported. - ADD: Delphi/CBuilder XE3 now supported. - ADD: Delphi/CBuilder XE2 now supported. - ADD: Delphi/CBuilder XE now supported. - ADD: Delphi/CBuilder 2010 now ...

    php.ini-development

    Output buffering is a mechanism for controlling how much output data ; (excluding headers and cookies) PHP should keep internally before pushing that ; data to the client. If your application's ...

    iOS.Programming.The.Big.Nerd.Ranch.Guide.5th.Edition.01

    Chapter 4. Text Input and Delegation Chapter 5. View Controllers Chapter 6. Programmatic Views Chapter 7. Localization Chapter 8. Controlling Animations Chapter 9. UITableView and ...

    Automate.the.Boring.Stuff.with.Python.Practical.Programming

    Chapter 4. Lists Chapter 5. Dictionaries and Structuring Data Chapter 6. Manipulating Strings Part II. Automating Tasks Chapter 7. Pattern Matching with Regular Expressions Chapter 8. Reading and ...

    Windows 8.1 Apps with XAML and C# Unleashed

    Chapter 4. Layout Chapter 5. Interactivity Chapter 6. Handling Input: Touch, Mouse, Pen, and Keyboard Part III: Working with the App Model Chapter 7. App Lifecycle Chapter 8. Threading, Windows, and ...

    Arduino.Android.Blueprints

    Chapter 4. Wi-Fi Smart Power Plug Chapter 5. Wi-Fi Remote Security Camera Chapter 6. Android Phone Sensor Chapter 7. Voice-activated Arduino Chapter 8. Control an Arduino Board via NFC Chapter 9. ...

    Mechanism of Controlling Cross-linking Polymerization Progress

    Mechanism of Controlling Cross-linking Polymerization Progress,栗方星,Sun Ruimin, A mechanism of controlling cross-linking polymerization process is reported in this paper. Chain propagation of ...

    Robot.Programming.A.Guide.to.Controlling.Autonomous.Robots

    Chapter 4 Checking the Actual Capabilities of Your Robot Chapter 5 A Close Look at Sensors Chapter 6 Programming the Robot’s Sensors Chapter 7 Programming Motors and Servos Chapter 8 Getting Started ...

Global site tag (gtag.js) - Google Analytics