03 Java basics _ Conditions and loops

Source: Internet
Author: User

 

03Java basics _ Conditions and loops

VThe nested if statement contains another if statement in the IF or else statement.

VIn Java, an else statement is usually the same as an if statement that is closest to it and not associated with else. It has nothing to do with the writing format.

Switch statement

VA switch statement is a multi-channel judgment statement that determines whether the expression value matches a value in the integer or character constant list.

VIf a match is found, the statement associated with the constant is executed.

VSwitch, case, and default are keywords, and the statement can be a simple or compound statement.

VThe expression after switch needs to be enclosed in parentheses, and the topic of the switch statement is enclosed in {}.

VThe data type of the computing expression must match the data type of the specified case constant.

VThe expression after the switch can contain any variable name or constant.

VOnly constants (integer and complex type) can be followed by a case ). // Menu Enumeration type

For Loop

VGeneral syntax of the For Loop:

For (1 initialization count; 2 conditional test; 4 re-evaluate the parameter)

{

3 Statement; // if it is a compound statement, {} cannot be omitted

}

VThe initialization count is a value assignment statement that sets the cyclic control variable before entering the loop.

VA conditional test is a relational expression that determines when to exit the loop.

VThe reevaluate parameter defines how the cyclic control variable changes each time a loop is executed.

VThe three parts of the For Loop must be separated by semicolons (;).

VThe statement that forms the loop body can be a single statement or a compound statement.

VThe for loop can contain partial definitions, or does not contain any definitions for (; num <100;) or (;

VUsing a for loop can not contain any loop body (statement), which helps improve the efficiency of some algorithms and create a time delay loop.

For (INT I = 0; I <xyz_value; I ++)

 

 

While Loop

VThe expression is generally a relational or logical expression.

VA statement can be an empty statement, a simple statement, or a block.

VIf the while loop contains a group of statements, it must be included in {}.

VAs long as the condition is true, iteration (traversal, loop) occurs)

 

Do... while loop

VThe semicolon after while cannot be omitted

VIn the do... while loop, the code in the loop body is executed once before the conditional test is executed.

VWhen the condition is false, the do while loop ends. The control is passed to the statement followed by the while statement.

Differences and summary of the three loops

VThe while and for statements judge the loop control conditions first, and the do-while statement then judge the loop control conditions. Therefore, the loop bodies of the while and for statements may not be executed at one time, the do-while statement must be executed at least once.

VThe three loops can handle the same problem and can be replaced by each other in general. However, in actual application, we need to select different loop statements based on the actual situation. The general principles are as follows:

§ If the number of cycles is determined before the loop body is executed, the for statement is generally used. If the number of cycles is determined based on the execution of the loop body, generally, the while statement or the do-while statement is used.

§ When the loop body is executed at least once, use the do-while statement. Otherwise, if the loop body may not execute the row at once, use the while statement.

Return Statement

Return expression;

VThe expression is optional.

VThe return statement is used to return data from a function.

VA return statement can contain a value that is returned to the caller so that the execution returns to the position where the function is called.

VMultiple Return statements can be used in a function. However, if the first return statement is used

Break statement

VUsed to terminate a case in a switch statement

VIt can also be used to suddenly terminate a loop in a loop.

VWhen a break statement is encountered in a loop, the loop ends immediately, and the control is passed to the statement after the loop.

VIn a multi-layer loop, break only jumps one layer outward

Continue statement

VThe continue statement can only be used in a loop to allow a closed loop to carry out the next iteration.

VWhen this statement is encountered, the remaining statements in the loop body are skipped and the control is passed to the reinitialization step.

VIn the while loop and do... In the while loop, program control passes a conditional test statement.

VIn the for loop, the continue control is directly handed over to the incremental part, and then the conditional test is executed.

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.