1, while loop
1. Statement definition format:
(1) Basic format
While (judgment condition statement){
Loop body statement;
}
(2) Extended format:
Initialization statement;
While (judgment condition statement){
Loop body statement;
Control condition statement;
}
2. A while loop can be equivalent to a for loop.
Example: print 10 helloworlds
public class WhileDemo1 { public static void main(String[] args) { System.out.println("========for Cycle 10 sentences HelloWorld==========="); for (int i = 1; i <= 10; i++) { System.out.println("HelloWorld"); } System.out.println("=======while Cycle 10 sentences HelloWorld============="); /* Initialization statement: int i=1; Judgment condition statement: I < = 10; Loop body statement: system out. println("HelloWorld"); Control condition statement: i + +; */ int i = 1; while (i<=10){ System.out.println("HelloWorld"); i++; } } }
3. Find the sum of 1 ~ 100 and realize it with the while loop
public class WhileDemo2 { public static void main(String[] args) { int sum1 = 0; for (int i = 1; i <= 100; i++) { sum1 = sum1 + i; } System.out.println(sum1); System.out.println("========while Cycle improvement================="); int sum2 = 0; int i = 1; while (i <= 100) { sum2 = sum2 + i; i++; } System.out.println(sum2); } }
4. What is the difference between a while loop and a for loop?
1. Although equivalent conversion can be made between them, if loops are made within a range during development, the for loop is preferred
2. When the number of cycles is uncertain, the while cycle is preferred, such as drinking water and eating grapes
Differences brought by scope:
3. After the for loop ends, the variables in the initialization statement cannot be accessed externally
4. After the while loop ends, the variables in the initialization statement can continue to be used
public class WhileDemo3 { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++) { sum = sum + i; } System.out.println(sum); // System.out.println(i); int sum2 = 0; int j = 1; while (j <= 100) { sum2 = sum2 + j; j++; } System.out.println(sum2); System.out.println(j); } }
5. The highest mountain in China is Mount Everest: 884800m. Now I have a large enough paper with a thickness of 1m. How many times can I fold it to a height no lower than Mount Everest?
public class WhileTest { public static void main(String[] args) { //Defines the initial paper thickness int thickness = 1; //Defines the number of times a variable receives folds int count = 0; //Because we don't know how many times to fold, we use while loop while (thickness <= 884800) { count++; thickness = thickness * 2; } System.out.println("Folded" + count + "Once, the thickness of the paper is:" + thickness); System.out.println("==============for Loop implementation============================"); for(int thickness1 = 1,count1=0;thickness1 <= 884800;){ thickness1 = thickness1 * 2; count1++; if(thickness1 >= 884800){ System.out.println("Folded" + count1 + "Once, the thickness of the paper is:" + thickness1); } } } }
2, Do while Loop
1. Statement definition format
(1) Basic format
do {
Loop body statement;
}While (judgment condition statement);
(2) Extended format
Initialization statement;
do {
Loop body statement;
Control condition statement;
}While (judgment condition statement);
2. While loop and do What is the difference between while loops?
The while loop first judges the condition to see if it is true. If it is true, execute the contents of the loop body
And do The while loop will first execute the contents of the loop body, and then judge the condition to see if it is true. If it is true, continue to execute.
public class DoWhileDemo1 { public static void main(String[] args) { int x = 3; while (x<3){ System.out.println("HelloWorld"); } System.out.println("========do...while loop============="); do{ System.out.println("HelloWorld"); }while (x<3); System.out.println("=========while Dead cycle==================="); boolean flag = true; while (flag){ System.out.println("HelloWorld"); } } }
3, Loop nesting
Print 4 rows and 5 columns of stars on the console*
1. To know how to print four rows and five columns *, you must first know how to print one row
2. Before that, we have not introduced how to print the same line of data. If you want to make multiple output statements on one line, you have to use another output format of java.
System.out.println(); Output and wrap
System.out.print(); Output does not wrap
public class XunHuanQianTaoDemo { public static void main(String[] args) { // System.out.println("*"); // System.out.println("*"); // System.out.println("*"); // System.out.println("*"); // System.out.println("*"); //java Another format for the output statement //System.out.print(); // System.out.print("*"); // System.out.println("*"); //Print 5 per line* // System.out.print("*"); // System.out.print("*"); // System.out.print("*"); // System.out.print("*"); // System.out.print("*"); //Add tabs to print 5 lines*,send*There is a gap between them, which is more beautiful // System.out.print("*\t"); // System.out.print("*\t"); // System.out.print("*\t"); // System.out.print("*\t"); // System.out.print("*\t"); //use for Loop improvement print a line of 5 stars* // for(int i=0;i<5;i++){ // System.out.print("*\t"); // } // System.out.println(); // for(int i=0;i<5;i++){ // System.out.print("*\t"); // } // System.out.println(); // for(int i=0;i<5;i++){ // System.out.print("*\t"); // } // System.out.println(); // for(int i=0;i<5;i++){ // System.out.print("*\t"); // } //use for Cycle improved again for (int j = 0; j < 4; j++) { // The outside loop controls the line for (int i = 0; i < 5; i++) { // The internal loop controls the columns System.out.print("*\t"); } System.out.println(); // Wrap after each printed line } System.out.println("================================================================"); /** * Requirements: Please output the following graphics * 1,To output the following graphics, you must first know how to output 5 rows and 5 columns, which we just learned * 2,Print the following figure by looking for rules * * * First line, I = 0, J < = I, print 1 * ** Second line, I = 1, J < = I, print 2 * *** The third line, I = 2, J < = I, prints 3 * **** Fourth line, I = 3, J < = I, print 4 * ***** Line 5, I = 4, J < = I, print 5 */ //Print 5 rows and 5 columns first for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print("*\t"); } System.out.println(); } System.out.println("=========================================="); for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*\t"); } System.out.println(); } System.out.println("==============Print nine rows and nine columns of stars=============="); for (int i = 0; i < 9; i++) { for (int j = 0; j <= i; j++) { System.out.print("*\t"); } System.out.println(); } System.out.println("=============Output the 99 multiplication table on the console================="); /* 1*1=1 2*1=2 2*2=4 3*1=3 3*2=6 3*3=9 ... 9*1=9 9*2=18 .............. 9*9=81 */ for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(i + "*" + j + "=" + (i * j) + "\t"); } System.out.println(); } } }