While loop
- (Inside is a loop condition is also an expression) Boolean expression a>b; Returns a result of true false
-
For and while are a type of loop
int sum=0;int i=11;(一)while(i<=10){ if(i==5){ continue;} sum+=i; //把水倒入水桶 System.out.println(i); i++; //倒入过后在增加一个水杯}(二)while(true){ if(sum==5050){break;}}System.out.println(sum);}
Dowhile Cycle
- Dowhile is until the type loop
-
A loop is executed first and then judged (until a condition is satisfied before the loop starts)
int i = 10;int sum = 0; do{ i++; if(i==5){ break;//continue;} sum+=i; //循环体 System.out.println(i);}while(i<10); //循环判断条件System.out.print(sum);
For loop
int sum = 0; int i = 1; //用水杯往水桶里面加水。加到100杯水停止就加 //加水加到50杯就不加了 //第50杯 杯水有毒,不加 for(i =1;i<=100;i++){ /*if(i==50){ break; //跳出本轮循环 }*/ if(i==50){ continue;//结束本次循环,继续下次循环} sum= sum+i;//不断往水桶里面加水sum+=i;}
- The break keyword can be used for any loop control structure, which causes the loop to terminate immediately. Jump out of this round cycle (in switch is the end of judgment)
- Continue end this cycle, start the next cycle
Double loop
(一)for(int i=0;i<8;i++){//外部循环一次,内部循环10次 for(int k =8;k>i;k--){ System.out.print(" ");} for(int j=0;j<8;j++){//外部循环控制行, 内部循环控制列 System.out.print("*");} System.out.print("\n"); (二)for(int i=0;i<8;i++){//外部循环一次,内部循环10次 for(int j=0;j<i+1;j++){//外部循环控制行, 内部循环控制列 System.out.print("*");} System.out.println();}
Three types of cycle differences
- The FOR Loop statement is suitable for looping programs that have a known cycle count
- While loop statement is suitable for looping programs with unknown loop times
- Dowhile loop statements (not much used) are still suitable for looping programs that have an unknown number of construction cycles, but at least one execution
Java Learning (iii)