Java BASIC program process control (below), java Process

Source: Internet
Author: User

Java BASIC program process control (below), java Process
Java program flow control (below)

This article separately sorts out the knowledge points of the loop structure:

The loop structure is divided into: for Loop, while loop, do... the while loop has three basic loop structures. In Versions later than JDK1.5, The foreach loop is provided for Traversing arrays and sets.

Four components of a loop statement:

  • Initialization
  • Loop condition Section
  • Loop body
  • Iteration part
For Loop:

For (initialization condition; loop condition; iteration part){

Loop body

}

1 public class TestFor {2 public static void main (String [] args) {3 // The basic for loop. Print a statement multiple times. 4 for (int I = 0; I <7; I ++) {5 System. out. println ("Hello World !! "+ I +" Times printing "); 6} 7} 8}

Exercise: print all the even numbers in 1-

1 public class TestFor {2 public void PrintNum () {3 for (int I = 1; I <= 100; I ++) {4 if (I % 2 = 0) {// obtain the remainder of pair 2. If it is 0, it indicates that it is an even number. Execute the print statement. Otherwise, the loop continues until the even number condition or I> 0 5 System is met. out. println ("I =" + I); 6} 7} 8} 9 public static void main (String [] args) {10 TestFor testFor = new TestFor (); 11 testFor. printNum (); 12} 13}

Exercise: write the code to meet the requirement of loop from 1 to 150 and print a value in each row. In addition, print "foo" in the line multiple of each 3 ", print "biz" in a row multiple of 5 ",

Print the output "baz" on multiple lines of 7"

 1 public class TestFor { 2     public void FooBizBaz(){ 3         for(int i=1; i<=150; i++){ 4             System.out.print(i+":"); 5             if(i%3 == 0){ 6                 System.out.print(" foo"); 7             } 8             if(i%5 == 0){ 9                 System.out.print(" biz");10             }11             if(i%7 == 0){12                 System.out.print(" baz");13             }14             System.out.println();15         }16     }17     public static void main(String[] args) {18         TestFor testFor = new TestFor();19         testFor.FooBizBaz();20     }21 }

Note: else if () {} cannot be used in this question, three fields cannot be printed simultaneously in the number of rows that meet the multiples of 3, 5, and 7, once a condition is met, the if statement below will not be executed to directly jump out of the current loop and execute the next loop.

There are also many small basic for loop algorithms, such as printing the sum of all the odd numbers from 1 to, and printing all the Daffodils (you can search for them by yourself). You can exercise these questions on your own, this helps to enhance the understanding of the for loop. I will not go into detail here.

While loop:

  Initialization Conditions

While (loop condition ){

Loop body

Iteration conditions

}

Example: output all the even numbers within 1-

1 public class TestWhile {2 public void evenNum () {3 int I = 1; // initialization condition 4 while (I <= 100) {// cyclic condition 5 6 if (I % 2 = 0) {7 System. out. println ("I =" + I); 8} // cycle body 9 10 I ++; // iteration condition 11} 12} 13 14 public static void main (String [] args) {15 TestWhile testWhile = new TestWhile (); 16 testWhile. evenNum (); 17} 18}

Note: A for loop and A while LOOP can convert each other because they have the same four parts, but the four parts are placed in different positions.

Another loop method of while loop:

Do... while loop:

Initialization Conditions

Do {

Loop body

Iteration conditions

} While (Cyclic condition );

From the above structure, we can see that the do... while loop is the first loop to determine whether the cycle conditions are met. If the conditions are met, the next loop is performed. If the conditions are not met, the loop is stopped.

Example: print all the even numbers in 1-

 1 public class TestDoWhile { 2     public void evenNum(){ 3         int i = 1; 4         do{ 5             if(i % 2 == 0){ 6                 System.out.println("i="+i); 7             } 8             i++; 9         }while(i <= 100);10     }11     public static void main(String[] args) {12         TestDoWhile testDoWhile = new TestDoWhile();13         testDoWhile.evenNum();14     }15 }

Do... the difference between while and while loops:

Do... even if the while LOOP initialization condition does not meet the loop condition, the loop body is executed once and then the loop condition is judged. Therefore, the loop body is executed at least once, while a while loop is executed only when the loop condition is met.

Nested loop

  As the name implies, a nested loop means another loop can be declared in another loop.

Nesting methods:

1. One or more for can be nested in a for loop;

2. One or more while loops can be nested in the while loop;

3. One or more while loops can be nested in the for loop;

4. One or more for loops can be nested in the while loop;

5. One or more for/while loops can be nested in the for loop;

6. One or more for/while loops can be nested in the while loop.

Example:

For nesting

1 public class TestForFor {2/* print out *************************** 6 **/ 7 public void forQianTao () {8 for (int I = 1; I <= 4; I ++) {9 for (int j = 1; j <= 5; j ++) {10 System. out. print ("*"); 11} 12 System. out. println (); 13} // This is a two-tier loop. I is used to control the number of printed rows. j is used to control the number of printed columns. 14} 15 public static void main (String [] args) {16 TestForFor testForFor = new TestForFor (); 17 testForFor. forQianTao (); 18} 19 20}

For other nested loops, see the preceding example. The structure is similar. You can perform exercises on your own. For example, you can print the 9-9 multiplication table through nested loops, or print a diamond pattern composed of asterisks. Each asterisks must have a space, these questions can enhance the understanding of nested loops.

 

 

 

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.