Java Programming ideas (Iv. control execution flow)

Source: Internet
Author: User
Tags goto

Java uses all of the process Control statements for C. The keywords involved include if-else, while, Do-while, for, return, break, and select statement switch. In Java, however, goto statements are not supported. However, you can still make a jump of type Goto, but there are a lot of limitations compared to the typical goto.

1, True, and false. All conditional statements use the true and false of conditional expressions to determine the execution path. All relational operators in Java can be used to construct conditional statements. But you cannot use a number as a Boolean, (in C, C + +, "true" is nonzero, "false" is 0), but not in Java, if you use a non-Boolean value such as if (a) in a Boolean test, you must first convert it to a Boolean value using a conditional expression, such as if (a! =0)

2, If-else. This is the most basic form of the control procedure. The else in which is optional.

if(boolean-expression)    statement or   if(boolean-expression)    statement   Else     Statement

3. Iteration statements. The (while, do-while, for) statements are repeated until the Boolean expression that controls it (booleanexpression) gets a "false" result.

Format of While loop: Whlie (boolean-expression)
Statement

Small tips (The static method in the math library random (), which is used to produce a double value between 0 and 1 (including 0, but excluding 1)

// do-while Format  Do     Statementwhile (boolean-expression)///Unlike while, the statement in Do-while is executed at least once, even if the expression is evaluated to false for the first time

// For loop format:  for (Initialization (initialization expression); boolean-expression(boolean expression), step (step operation))    statement

4, comma operator. 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 sections of the control expression, you can use a series of comma-delimited statements.

For example
for (int i=1,j=i+10;i<5;i++,j=j*2)

5, foreach syntax. For arrays and containers, you do not have to create an int variable to count the sequence of access items, and foreach automatically generates each item. can also be used with any Iterable object.

6, return. Returan Use of Keywords: Specifies what value a method returns (assuming it does not have a void return value), exits the current method, and returns that value.

7, break and continue. In the body part of any iteration statement, you can use break and continue to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. Continue then stops executing the current iteration, then bounces back to the start of the loop and begins the next iteration.

8. Two forms of infinite cycle. while (true) and for (;; )。

9. The infamous goto. Use tags in java.

 lable1:outer -iteration{inner - Span style= "COLOR: #000000" >iteration{ //        ... break   //        ...  continue  ;//jumps out inner This time, continues Innner  //        ...  continue   label1;//jumps Innner,outer this time, continues outer next time  //        ...  break             label1;//jump out of Outer}} 

The Tips:break interrupts the For loop, and the increment expression is not executed until the end of the For loop is reached. Both break and continue can only interrupt the inner loop, except that it will not start the next loop, but continue will continue.

1) The normal continue will be returned to the beginning of the inner loop and continue execution.

2) The tagged continue will reach the position of the label and re-enter the loop that is immediately behind that label.

3) A normal break breaks and jumps out of the current loop.

4) A tagged break breaks and jumps out of the loop that the label refers to.

The only purpose of using tags is because of the existence of loop nesting.

10, switch. Select the statement.

Switch(integral-selector) {
CaseIntegral-value1:statement; Break ; CaseIntegral-value2:statement; Break ; CaseIntegral-value3:statement; Break ; CaseIntegral-value4:statement; Break ; CaseIntegral-value5:statement; Break ; default: statement;}

The switch statement is for multi-channel selection. However, it requires a selection factor and must be an integer value such as int or char. If you use other characters such as strings or floating-point numbers as the selection factor, they will not work in the switch statement.

The switch statement is perfectly combined with the enum. About enum. https://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html this blog is written in very detailed, interested players can learn a bit.

Java Programming ideas (Iv. 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.