Java Basics Summary-Process Control

Source: Internet
Author: User

Java Process Control
* Sequential structure-the most common execution, and the statements are executed sequentially in the order in which they appear.
* Judging the structure--to judge the condition and execute the corresponding statement. There are three kinds of common forms
< if (judging condition) {statement;} --Executes the statement for true, otherwise does not execute
< if (judging condition) {statement;} else{statement;} Note that this structure is a whole and only executes a branch, for true-if control statements, false-execute else control statements.
< if (judging condition) {statement;} else if (judging condition) {statement;} else{statement;} --Pay attention to multiple condition judgments-this structure is also a whole, will only perform one, and will not judge the other branches when satisfied.
Attention:
If structure if you only control one statement can not write curly braces, beginners best write parentheses.
A statement--possibly a single line statement/compound statement--curly braces to expand the statement
The IF-ELSE structure can be replaced with a certain degree of three-mesh operator, which can be replaced by a result that has a value after execution.
If a judgment structure writes too many conditions-it may be necessary to combine the conditions

* Local code block--defined in a function, a group of statements that are wide with curly braces
The main role-is to control the life cycle of local variables, timely memory

* Select Structure-give a limited alternate path
Basic structure:
switch (variable value) {
Case value 1: statement; break;
Case value N: statement; break;
Default: statement; Last break is optional)
}
Feature: Variable types are qualified--short,int,byte,char
execution begins with the most recent case, the writing order does not require
The switch statement to end the standard: encountering curly braces ( Natural end) or break (jump out of the switch statement)
Note:
< The execution process jumps out of the switch statement after a match from the nearest case, and if there is no break, it will not be judged that the following case--will be executed until break/ The default control statement to the end.
< Executes the Dafault statement without a match (writes end directly), or it executes to break or curly brace ends.
< break has a significant impact on the execution of a switch, so use
the difference between <if and switch
If you can judge values, intervals, Boolean variables
switch can only judge the value, Recommended for limited switch-because all of the alternatives are loaded into memory, it executes faster.

* Cyclic structure-The purpose of this is to solve repetitive operations, there are three types of cyclic structures
while (conditional expression) {The statement that participates in the loop (including the post-loop action statement);} --in fact, if very much like, only the difference is the multiple judgments of the same condition
For (1 initialization expression; 2 loop control statement; 3 post-loop action expression) {4 statement participating in the Loop;}
The execution flow for a for loop is first initialized-to determine whether to enter a loop-Execute loop body-post-action expression (end of a loop), where the second of three expressions must be Boolean, the remaining two are not required, and the number of variables can be multiple, separated by commas.
do{Statement}while (conditional expression); Nothing special--it's going to be executed at least once.
--How to write loops--
1. How to solve problems with plain English analysis
2. Parse the statement to be repeated-put into the loop
3. Determine the control conditions of the cycle
--what kind of loop to choose--
1. Usually determine the number of cycles generally select for loop
2. Cannot determine the number of cycles to select while loop
3. The loop body must be executed at least once select the Do and loop
--Cycle control mode--
1. Counter idea
2. Tag variable control loops
3. Sometimes two combinations are required-for example, to find primes
--loop nesting--remember the Big trap small circle (usually one-to-many ideas)--analysis of what the law
--Other loop control statements--
1.break-out of the current loop-application range (switch, usually combined with if in the loop)
Note that combining if is used only to control when a break is working on a loop, and to jump out of the loop in a nested loop
2.continue-end of the cycle into the next cycle (use range-loop)
--Be careful not to judge with floating-point number equals, floating-point number is approximate processing--

Java Basics Summary-Process 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.