Thinking in Java notes (fourth. Control execution flow)

Source: Internet
Author: User

The fourth chapter controls the execution process

All the Process Control statements for C are used in Java. The keywords involved in Java include if-else,while,do-while,for,return,break,continue and the SELECT statement switch. However, Java does not support GOTO statements (the statement raises many objections), but Java can still jump like Goto.

4.1 True and False

All conditional statements use the true and false of conditional expressions to determine the path of execution. Java does not allow us to use a number as a Boolean value, although C and C + + are allowed ("not 0" is true, "0" is false).

4.2 If-else

The If-else statement is the most basic form of the control program process. Where else is optional. There are two ways to use it:

if(boolean-exp){    ...}if(boolean-exp){    ...} else {    ...}
4.3 iterations

While,do-while,for are used to control loops and sometimes divide them into iterative statements (iteration statement). The statement is repeated until the condition is met or the condition is not satisfied. The while statement has the following format:

while(boolean-exp){    ...}

At the beginning of the cycle, the value of the BOOLEAN-EXP is computed once, and it is computed again before the next iteration of the statement begins.

4.3.1 do-while

The only difference between do-while and while is that statements in Do-while are executed at least once.

4.3.2 For Loop

The For loop is initialized before the first iteration, followed by a conditional test, stepping in some way after the end of each cycle.

4.3.3 comma operator

The comma operator (not the comma delimiter), the only place in Java where the comma operator is used is the control expression for the For loop. In the initialization and step control section of the control expression, a series of comma-delimited statements can be used;

4.4 foreach Syntax

Java SE5 introduces a more concise for statement for arrays and containers that can easily traverse each item.

public class Test{    public static void main(String[] args){        Random rand = new Random(47);        float f[] = new float[10];        for(int i = 0; i < 10; i ++)             f[i] = rand.nextFloat();        for(floar x : f)            System.out.println(x);    }}

The foreach statement can be tried on any object that implements the Iterable interface. Using foreach not only saves time when you enter code, but more importantly, it's much easier to read, it's much easier to read, and it's easy to read, so you can see what you're doing, rather than for(int i=0; i<100;i++) the details that tell people how to do it.

4.5 return

There are several keywords in Java that indicate unconditional jumps, return, break, continue, and a tag similar to Goto in other languages.

Return has two uses:

    • On the one hand, specify what value a method returns (assuming it has no void return value)
    • On the other hand, it causes the current method to exit and returns that value.
4.6 Break and continue

In the body part of any iteration statement, you can use break and continue to control the flow of the loop. The break keyword is used to forcibly exit a loop without executing the rest of the loop. The continue is a loop that stops the current execution and starts the next iteration of the loop directly. The following example:

public class Test {    public static void main(String[] args) {        for(int i=0; i<100; i++) {            if(i == 74) break;            if(i%9 != 0)continue;               System.out.println(i + " ");        }    }}

The above output results are:

0 9 18 27 36 45 54 63 72

At 74, the break comes out, and when I cannot be divisible by 9, continue (that is, the next cycle).

4.7 Infamous Goto

The goto statement actually originates from the program control of the assembler: If condition A is set, jump here; otherwise jump there . If you read the assembly code that was eventually generated by the compiler, you will find that the program contains a lot of jumps. (the Java compiler generates its own "assembler code", running on top of the Java virtual machine, rather than directly on the CPU hardware).

Goto is a jump above the source level. Not much introduced, it is best not to use it will make the source code becomes difficult to read.

4.8 Switch

Switch is also sometimes planned as a choice statement . Depending on the value of an integer expression, the switch statement can be executed from a series of code, in the following format:

switch(integeral-selector) {    case integral-value1 :        statement1 ;        break;    case integral-value2 :        statement2 ;        break;    case integral-value3 :        statement3 ;        break;    default:        statement4;}

INTEGERAL-SELECOTR is an expression that produces an integer value, and switch compares the result to each integral-value (integer value). If a match is found, execute the corresponding statement below. If no matching statement is found, the block of statements contained in default is executed.

Each case has a break at the end; The statement this allows the execution process to jump to the end of the switch body. If there is no break in the case, then the subsequent scenario statement continues, regardless of whether the case is satisfied until the first break is met.

Thinking in Java notes (fourth. Control execution flow)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.