Control Structure (bottom), control structure (

Source: Internet
Author: User

Control Structure (bottom), control structure (

3. Loop Structure

During programming, a code is often executed repeatedly until the conditions are met, and the loop structure is used here. Java supports three types of loop statements: for, while, and do-while.

  1. for statement

The for loop statement is applicable to programs that explicitly know the number of loops. the syntax of the for statement is as follows:

For ([initial value expression]; [condition expression]; [step expression]) {loop body statement ;}

Note: 1. The initial value expression is executed only once during the loop process and is usually used to initialize the loop.

2. the three expressions of the for statement are optional. When the value expression is null, the initial value must be assigned to the loop control variable before the for statement. When the condition expression is null, the default conditions are true. When the step expression is empty, if no cyclic variable is operated on in the loop body statement, the value of the cyclic variable is always the initial value.

Example:

public class Test{public static void main(String[] args){int i,sum = 0;for(i = 1;i<=10;i++){sum = sum +  i;}System.out.println("sum--->"+sum);}}

For Loop statements can also be nested, and other for statements are nested inside a for statement. Nested loop statements can be used to perform complex operations or print regular patterns. The syntax is as follows:

For ([initial value expression 11]; [value expression 12]; [step expression 13]) {for ([initial value expression 21]; [value expression 22]; [step Expression 23]) {inner loop body statement ;}}

Example of printing a multiplication table:

public class Test{public static void main(String[] args){int sum ;for(int i = 1;i<10;i++){for(int j = 1;j<=i;j++){sum = i*j;System.out.println(i+"*"+j+"="+sum+" ");}System.out.println();}}}

  2. while statement

The while statement is applicable to a program that does not know how many times a loop is executed, but only knows the loop execution conditions. Syntax:

While (condition expression) {loop body statement ;}

The execution process of the while statement is: first judge the value of the conditional expression. If the value is true, execute the loop body statement, and then return to judge the value of the conditional expression, when the value of the conditional expression is false, the while LOOP body jumps out. If the value is false, the loop body statement is skipped and other statements following the loop body statement are directly executed.

Example (obtain 1 ~ 10 and ):

Public class Test {public static void main (String [] args) {int sum = 0; int I = 1; while (I <= 10) {sum = sum + I ;} system. out. println ("1 ~ 10 integers: "+ sum );}}

    3. do-while statement

The do-while statement is applicable to a program that wants to execute the loop body once and cannot determine the number of times the loop is executed but knows the loop execution conditions in advance. Do-while example:

Public class Test {public static void main (String [] args) {int sum = 0; int I = 1; do {sum = sum + I; I ++ ;} while (I <= 10); System. out. println ("1 ~ 10 integers: "+ sum );}}

Note: 1. Three types of loops can be nested.

2. The difference between the do-while loop statement and the while LOOP statement is that the loop body in the do-while loop is executed at least once, while the loop body in the while LOOP may not be executed at once.

4. Jump Structure

The jump structure mainly relies on Jump statements. The jump statement is used to transfer control to other parts of the program. The java language supports three jump statements: break, continue, and return.

  1. break

In java, the break statement has two main purposes: first, in the switch statement, the switch structure is forcibly exited and the statement after the switch structure is executed; the second is to force exit the loop in the loop statements (for, while, and do-while. In a loop statement, the break statement is generally used with the if statement. when certain conditions are met, the break statement jumps out of the loop. In a multi-tier loop, the break statement only jumps out of the current loop, instead of jumping out of the entire loop. Example:

public class Test{public static void main(String[] args){int sum =0;int i = 0;for(i = 1;i<=10;i++){sum = sum + i;if(i==5){ break;}}System.out.println("sum = "+sum);}}

  2. continue

The continue statement can only be used in loop statements (for, while, and do-while). It skips the unexecuted statements in the loop body and starts the next loop again. The difference between the continue and break statements is that when the break stops the execution of the loop body, it jumps out of the current loop statement and continues to execute; while the continue only stops the execution of the loop body after the continue, next, jump to the starting point of the loop and re-execute it, for example:

public class Test{public static void main(String[] args){int sum =0;int i = 0;for(i = 1;i<=10;i++){if(i<=5){ continue;}sum += i;}System.out.println("sum = "+sum);}}

  3. return

The return Statement in java is usually located in the last row of the method body. It has two functions: one is to return a value of the specified type of method, that is, return with a value; the other is to end the execution of the method, that is, return without a value. Syntax:

1. return;

2. return expression;

Example:

Return; // return without a value

Return false; // return false

Return x-y; // return the value of x-y.

Return larger (); // returns the computation result of the larger () method.

Note: If the data type of the returned value is different from the type of the returned value in the method declaration, use automatic conversion or forced type conversion to keep the type consistent.

 

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.