Java learning from scratch (loop structure), java from scratch

Source: Internet
Author: User

Java learning from scratch (loop structure), java from scratch
1. Loop Structure the loop structure determines the number of executions of a program section based on the judgment condition. This program section is called a loop subject. 2. While loop while is a loop statement and a condition judgment statement. When you do not know how many times a loop is executed, you need to use the while loop. The while LOOP format is as follows:While (loop condition judgment ){Statement 1;Statement 2;...Statement n;Change cycle conditions;}Example 1: use While to accumulate data within 100

Package loop; public class test1 {public static void main (String [] args) {// use While to perform the accumulate operation within 100 int sum = 0; // define the value of int I = 1; // define the integer variable I while (I <= 100) {sum + = I; I ++;} System. out. println ("sum of integers less than 100:" + sum );}}

Result: The sum of integers less than 100: 5050

While is to first determine whether the condition is true. If it is true, it enters the loop. If it is not true, It skips the loop.

Example 2: change the value of the same as I to 101
Package loop; public class test {public static void main (String [] args) {// use While to perform the accumulate operation within 100 int sum = 0; // define the value of int I = 101; // define the integer variable I while (I <= 100) {sum + = I; I ++;} System. out. println ("sum of integers less than 100:" + sum );}}

Result: The sum of integers less than 100 is 0.

3. do-while loop do... While loops are also used when the number of execution cycles is unknown, while loops and do... The biggest difference between a while loop is that before entering a while loop, the while statement first tests and judges whether the conditions are true or false, and then determines whether to execute the loop body, And do... The while loop is "Let's talk about it first". Each time we execute a loop body first, and then test and judge whether the condition is true or false. Therefore, no matter what the condition of the loop is, use do... During a while LOOP, at least one loop body is executed. statement format: Do { Statement 1; Statement 2; .... Statement n; Changing cycle conditions; } While (loop condition judgment ); Example 3: Use do-While to accumulate values within 100
Package loop; public class test {public static void main (String [] args) {// use While to perform the accumulate operation within 100 int sum = 0; // define the value of int I = 1 when the variable is saved; // define the integer variable I do {sum + = I; I ++;} while (I <= 100 ); system. out. println ("sum of integers less than 100:" + sum );}}

Result: The sum of integers less than 100: 5050

4. for Loop for while and do... For the while loop, the operation does not have to explicitly know the number of cycles. Knows the number of cyclesThen you can use another loop statement, for loop. The format is as follows: For (initial values of values; judgment conditions; increase or decrease of values ){ Statement 1; .... Statement n; } Example 4: Use for to accumulate values within 100
Package loop; public class test2 {public static void main (String [] args) {// use for the accumulate operation within 100 int sum = 0; // define the accumulative value of the variable for (int I = 1; I <= 100; I ++) {sum = sum + I;} System. out. println ("sum of integers less than 100:" + sum );}}

Result: The sum of integers less than 100: 5050

4.1 loop nesting Example 5. for Loop printing 99 multiplication table
Package loop; public class test3 {public static void main (String [] args) {// print the 99 multiplication table for (int I = 1; I <= 9; I ++) {for (int j = 1; j <= I; j ++) {System. out. print (j + "*" + I + "=" + (j * I) + "\ t");} System. out. println ();}}}

Result:

1*1=1    1*2=2    2*2=4    1*3=3    2*3=6    3*3=9    1*4=4    2*4=8    3*4=12    4*4=16    1*5=5    2*5=10    3*5=15    4*5=20    5*5=25    1*6=6    2*6=12    3*6=18    4*6=24    5*6=30    6*6=36    1*7=7    2*7=14    3*7=21    4*7=28    5*7=35    6*7=42    7*7=49    1*8=8    2*8=16    3*8=24    4*8=32    5*8=40    6*8=48    7*8=56    8*8=64    1*9=9    2*9=18    3*9=27    4*9=36    5*9=45    6*9=54    7*9=63    8*9=72    9*9=81    
5. The break statement can force the program to interrupt the loop. When the program runs the break statement, it will exit the loop and continue executing the next statement outside the loop, if the break statement appears in the nested loop, the break statement only jumps out of the current loop. Take the for loop as an example. When the program runs break when there is a break statement in the loop body, it will leave the loop body and continue to execute the statement at the outer layer of the loop. Example 6: Implement output 1-10 and use break when 4 is encountered;
Package loop; public class test4 {public static void main (String [] args) {// implement output 1-10. When 4 is encountered, the program exits for (int I = 1; I <= 10; I ++) {if (I = 4) {break;} System. out. print (I + "");} System. out. println ("loop end ");}}

Result: The cycle ends.

6. The continue statement can force the program to jump to the starting point of the loop. When the program runs the continue statement, it stops running the remaining loop entity, but returns to the beginning of the loop to continue running. Take the for loop as an example. The loop body contains a continue statement. When the program runs on the continue, it returns to the starting point of the loop and continues to execute some statements of the loop body. Continue is the example 7 of skipping the current loop and entering the next loop. The output is 1-10 as above. When 4 is encountered, use continue;
Package loop; public class test4 {public static void main (String [] args) {// implement output 1-10. When 4 is encountered, use continue for (int I = 1; I <= 10; I ++) {if (I = 4) {continue;} System. out. print (I + "");} System. out. println ("loop end ");}}

Result: The cycle ends.

VII. return Statement

End the execution and exit of the current method, and return the statement that calls the method.

In the example, output 1-10 is the same as above, and return is used in case of 4;
Package loop; public class test4 {public static void main (String [] args) {// implement output 1-10. return for (int I = 1; I <= 10; I ++) {if (I = 4) {return;} System. out. print (I + "");} System. out. println ("loop end ");}}

Result: 1 2 3

We can see that there is no output for "loop End". When I = 4, return is executed to end the execution of the entire method.

 

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.