Java from small white to getting started, Day3 (program flow control)

Source: Internet
Author: User
Tags case statement

Program flow is the order in which the program needs to execute.

Change the process of program control: There are 2 methods, branching (selection) and looping.

Branching: Multiple choices, with if structure, switch structure.

Loops: Run repeatedly with while, Do-while, For,foreach (used when an array is traversed).

Variable name: Defines a variable whose name cannot be the same as that of a variable in the same scope.

Break: The keyword can be used to control any loop structure so that the loop terminates immediately.

Continue: The keyword can be used to control any loop structure so that the loop immediately jumps to the next iteration of the loop.

Multi-digit redundancy: for example, 1234, take out thousands: 1234/1000%10,

Out of the Hundred: 1234/100%10,

Remove 10 digits: 1234/10%10,

Remove single digit: 1234%10.

Operators:&& and | |, respectively, are short-circuit and, short-circuiting, or operators.

If and If/else structures: in If/else, else if can repeat multiple, but else there is at most one.

Code structure: if (condition 1) {code 1;}//If condition 1 is true, execute code 1

else if (condition 2) {code 2;}//If condition 1 is false, condition 2 is determined, and if true, code 2 is executed

else{code 3;}//If the condition is False, execute code 3.

Switch: Code structure: A switch (variable or expression)//variable is supported only (byte, short, int, Boolean, String (JDK1.7)), the case value is the same as the variable data type, and must be constant or literal.

{

Case value 1; code 1; break;//if the variable or expression value is 1, execute code 1, and then stop the loop. If there is no break, proceed to the next case statement until a break is encountered.

Case value 2; Code 2; break;//if the value is not equal to 1, determine whether the variable or expression value is 2, or 2, execute code 2, and then stop the loop.

Default: Code 3//If the variable or expression value is not 1 nor 2, then code 3 is executed.

} Switch features: Higher performance than if structure.

While: Code structure, while (condition) {loop body;}//Judging condition, if true, executing loop body, then judging if condition is true, if true, execute loop body. Judge the condition repeatedly and execute the loop body. Until the condition is false, stop the loop.

Do/while: Code structure, do{loop Body,} while (condition),//execute loop body First, then determine condition, if true, execute loop body, then determine if condition is true, if true, execute loop body. Judge the condition repeatedly and execute the loop body. Until the condition is false, stop the loop.

For: Code structure, for (expression 1; Boolean expression; expression 2) {loop body;}//First execution of expression 1 (1 execution only), in judging Boolean expression, if Boolean is true, the loop body is executed, (if Boolean is false stop for loop), after the loop body is executed, Executes the expression 2, in which the Boolean is determined, and if the Boolean is true, the loop body is executed; After executing the loop body, repeat the expression 2,boolean until the Boolean is false to stop the For loop.

Branching and looping can be cross-used as long as the individual code and requirements are followed.

Java from small white to getting started, Day3 (program flow control)

Related Article

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.