Java program Ape's flow control and array

Source: Internet
Author: User

 Branch statements

The if parenthesis can only be a logical expression, that is, the value returned by the expression can only be true or false.

Blocks of code are enclosed in curly braces, and a block of code is usually executed as a whole (unless you encounter a keyword such as return, break, continue, or an exception).

The If......else statement has a basic rule: always prioritize conditions that are small in scope before handling them.

The switch statement consists of a control expression and multiple case labels, unlike the IF statement, the data type of the control expression after the switch statement can only be a byte, short, char, int four integer type. Enum type and java.lang.String type (not allowed from Java 7), cannot be a Boolean type.

Switch (expression) {   case  conditionl:   {     statement (s);       Break ;     }            ............     
  default;
{
statement (s);
}
}

  Each code block after each case label has a break after it, and the statement, which has a very important meaning, is that the Java switch statement allows the case to be followed by a break statement, but this may introduce a trap that will continue to execute. The switch statement evaluates the expression and then compares the expression to the value of the case label, and once an equal value is encountered, the program executes the code following the case and no longer determines whether the condition of the following case, default label matches.

Java7 enhances the function of the switch statement, allowing the control expression of the switch statement to be a scalar or expression of type java.lang.String-- You can only have java.lang.String types, and you cannot make StringBuffer or StringBuilder of the two string types.

Loop structure

  While statement

  The while loop syntax format is as follows:

[Init_statement]  while (test_expression); {   statement;   

  The while loop evaluates the text_expression loop condition before each execution, and runs the loop body portion if the loop condition is true. The above iteration statement is always at the end of the loop body, and only when the loop body executes, the while loop executes the iteration statement.

The loop body portion of the while loop is merged with the iteration statement, with only one line of code, which can be omitted from the following curly braces, but is not recommended. If you omit the curly brace, the loop condition is controlled to the first semicolon immediately following the condition of the loop.

Use the While loop statement to ensure that the loop condition has false, otherwise the loop will be called a dead loop.

int i= 0; while (I <); {    System.out.println ("Endless cycle of death! "+ i);    }

  You must follow a semicolon after the while loop condition, followed by a semicolon after the loop condition, followed bya semicolon after the loop condition; it's important to say three times.

  In a Java program, a separate semicolon represents an empty statement, and an empty statement that doesn't do anything.

Do......while statements

The difference between the Do......while loop and the while loop is that the while loop is the first to judge the loop condition, and if the condition is true, the loop body is executed, and the Do......while loop executes the loop body before judging the loop condition, and if the loop condition is true, the next loop is executed, otherwise the loop is aborted.

[Init_statement]  Do {   statement;     while (test_expression);

  A semicolon is required after the loop condition of the Do......while loop, which indicates that the loop ends.

The loop body of the Do......while loop is executed at least once.

  

For loop

 for ([init_statement]; test_expression; [Iteration_statement]; ) {     statement;    }

  The loop iteration statement for the For loop is not put together with the loop body, so the loop iteration statement will be executed even when the loop body is executed with the Continue statement ending the loop.

The initialization statement for the For Loop has only one, and the loop condition is just a simple Boolean expression. In fact, a for loop allows multiple initialization statements to be made at the same time, and a loop condition can also be an expression that contains a logical operator

 for (int i = 0, j = 0; i < && J < Ten; i++, J + +) {        statement    }

  The loop variable can be modified by the For loop body, but this may result in a dead loop, so it is not recommended to modify the value of the loop variable (also called the Loop counter) in the loop body, or it will increase the likelihood of a program error. If you really need to access and modify the loop variable, we recommend redefining a temporary variable, assigning the loop variable to a temporary variable, and then modifying the value of the temporary variable.

The two semicolons within the for loop parentheses are required, and the initialization statement, the loop condition, and the iteration statement parts can be omitted. However, if the loop condition is omitted, then this loop condition defaults to True and is trapped in a dead loop.

You can define the initialization condition outside the loop body and place the loop iteration statement in the loop body, which is similar to the previous while loop

int i = 0;  for (; i <;) {        System.out.println (i);        I+ +;    }

  There is also a role in defining the initialization statement for the For loop before the loop: You can expand the scope of the variables defined in the initialization statement. A variable defined in a For loop whose scope is valid only within that loop, and after the For loop is aborted, the variables are inaccessible. If you need to use the values of these variables outside of the loop, you can take the above approach. In addition, you can define an additional variable to hold the value of the loop condition.

int temp = 0;  for (int i = 0; i < i++) {        statement        // save loop variable temp with temp        = i;    }

  Nested loops

Put a loop inside another loop. A nested loop is formed, and when the program encounters a nested loop, if the outer loop condition allows, the loop body of the outer loop is executed, and the inner loop is executed by the loop body of the outer loop--only the inner loop needs to execute its own loop body repeatedly. When the memory loop execution ends and the loop body of the outer loop finishes, the loop condition of the outer loop is computed again, deciding whether to execute the loop body of the outer loop again.

  

Ending a loop with break

Break is used to completely end a loop and jump out of the loop body.

The break statement can not only end its own loop, but also directly end its outer loop. At this point, you need to immediately follow a label, which identifies an outer loop.

The label in Java is a (:) identifier followed by a colon. Java tags can only be used in the loop body.

 //  outer loop, outer as identifier  outer;  for  (int  i = 0; i < 5; i++< Span style= "color: #000000;" ) { for  (int  j = 0; j < 3; j++< Span style= "color: #000000;"            ) {System.out.println ( "Print values for J and I"  if  (j = = 1) { //  jump out of the loop identified by the outer label  break   outer; }            }    }

  The label after break must be a valid label, meaning that the label must be defined before the loop in which the break statement is located, or before the outer loop of the loop in which it is located. Break defaults to the end of its label.

 Public classhh{ Public Static voidMain (string[] args) {//Please note that the outer is followed by a colon.outer: for(inti = 0; I < 5; i++){             for(intj = 0; J < 3; J + +) {System.out.println ("Print values for J and I" + "i =" + i + "J =" +j); if(J = = 1 ){                        //jump out of the loop identified by the outer tag                     Breakouter; }            }        }    }}

  Use continue to ignore the remaining statements in this loop

The continue can also be followed by a label that skips the remaining statements of the loop that the label identifies as a cycle, restarting the next loop.

  

Using the return End method

The return keyword is not intended to end a loop, and the return function room ends with a method. When a method executes to a return statement (The return keyword can also be followed by variables, constants, and expressions), this approach is ended.

  

  

  

  

Java program Ape's flow control and array

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.