2.4.1 Cycle control

2.4.1.1 the while loop
packageJava base 03_ loop structure;/* whlie loop format: while (judge condition) {loop; } * /
/* * Requirement: loop prints 10 times "while loop + I" */
public class While_Demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i =0;
		while(i<10) {
			System.out.println("While loop"+i+"Time");
			i++;
		}
/* * requirement: complete the sum of 1=100 * */
		int sum = 0;
		int x = 1;
		while(x<=100) { sum+=x; x++; } System.out.println(sum); }}Copy the code
2.4.1.2 do – while loop
packageJava base 03_ loop structure;Do {// execute the body of the loop}while (judge the condition); // Select */
/* * The difference between a while loop and a do-while loop * A while loop evaluates the condition before executing the body; * do_while loop, execute the body statement first, then check whether the conditional expression is true. * /

public class Do_While_Demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i= 1;
				do {
					System.out.println("Execute the do body first."+i);
					i++;
				}while(i<10);
				/* * add 1 to 100 with do-while */
				int sum=0;
				int x = 1;
				do {
					sum+=x;
					x++;
					System.out.println(sum);
				}while(x<=100);
				System.out.println("Final summation:"+sum); }}Copy the code
2.4.1.3 for loop
forInitialization statement; Judgment statement; Control statement) {loop statement body; } the sampleDemo
for(int j=0; j<=10; j++){
System.out.println("Number of cycles"+j); } Execution process:1: Executes the initialization statement2: Executes the condition statement to see if its return value istrueorfalseIf it istrue, execute the loop body statement. If it isfalse, ends the loop.3: Executes the loop body statement4: Executes the control condition statement5: back to the2Step continue executionCopy the code
packageJava base 03_ loop structure;/* for(initialization statement; Judgment statement; Control statement) {loop statement body; } example Demo for(int j=0; j<=10; J ++){system.out.println (" loop number "+j); } * /
public class For_Demo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		for(int i=0; i<=10; i++) { System.out.println("This is the For loop."+i+"Time");
		}
		/* * Requirements: 1-10 multiplications */
		// Method 1:
		int x =1;
		for(int i=1; i<=10; i++) { x*=i; }System.out.println(x);/ / output 3628800
		
		// Need: Find the sum of even numbers between 1 and 100, use for+if
		int sum=0;
		for(int i =1; i<=100; i++) {if(i%2= =0) {
				sum+=i;
			}
		}
		System.out.println(sum);/ / 2550
		// Method 2:
		int sum2=0;
		for(int i=0; i<=100; i+=2) {
			sum2+=i;
		}
		System.out.println(sum2);
		A narcissus number is a 3 digit number in which the sum of the 3 powers of the numbers in each place equals the narcissus number itself (e.g. 1^3+5^3+3^3=153) * * Output 100-1000 daffodils * 153=1*1*1+*3*3*3+5*5*5 * question: how do I get the tens, hundreds, and units digits? * * Bits: 135%10=3 * Tens: 153/10%10=5 * hundreds: 135/100=1 */
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --");
		int count =0;
		for(int i=100; i<1000; i++) {int ge=i%10;
			int shi =i/10%10;
			int bai = i/100;
			
			if(i==(ge*ge*ge+shi*shi*shi+bai*bai*bai)) { System.out.println(i); count++; } } System.out.println(count); }}Copy the code

The difference between a while loop and a for loop:

Use differences: If you want to continue using the variable that controls the condition after the loop ends, use while, otherwise use for.

2.4.1.4 Double loop

Double loop: Be sure to understand which loop executes first, which loop ends first, and which loop executes again.

packageJava base 03_ loop structure;/* * Double loop: * Requirements: * Please enter a star (*) pattern with 4 rows and 5 columns, using the for loop. * Renderings: * ***** * ***** * ***** * ***** */
public class ForForDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Be sure to understand which loop executes first, which loop ends first, and which loop executes after
		
		for(int i =0; i<4; i++) {for(int j = 0; j<5; j++) { System.out.print("*");
			}
			System.out.println();
		}
		/* * Requirements: Please enter the following shapes: ***** ***** ***** ***** */
		for(int i=0; i<=5; i++) {for(int j=0; j<i; j++) { System.out.print("*");
			}
			System.out.println();
		}
		/* * Print the multiplication table with the for loop */
		for(int i = 1; i<=9; i++) {for(int j=1; j<=i; j++) { System.out.print(j+"*"+i+"="+i*j+"\t"); } System.out.println(); }}}Copy the code