java loop statement

1 Loop statement for

For loop statement format:
for (initialization expression ①; Boolean expression ②; step expression ④)
{loop body ③}
execution flow execution sequence: ①②③④> ②③④> ②③④ ... ②Unsatisfied.
①Responsible for completing the initialization of the loop variable
②Responsible for judging whether the loop condition is satisfied, and jumping out of the loop if it is not satisfied ③Specifically
executed statement
④After the loop, the change of the variable involved in the loop condition
Insert picture description here

public static void main(String[] args) {
 //控制台输出10次HelloWorld,不使用循环 
 System.out.println("HelloWorld");
  System.out.println("HelloWorld"); 
  System.out.println("HelloWorld");
  System.out.println("HelloWorld"); 
  System.out.println("HelloWorld");
   System.out.println("HelloWorld"); 
   System.out.println("HelloWorld"); 
   System.out.println("HelloWorld");
    System.out.println("HelloWorld");
     System.out.println("HelloWorld"); 
     System.out.println("‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐"); 
     //用循环改进,循环10次 
     //定义变量从0开始,循环条件为<10 for(int x = 0; x < 10; x++) { System.out.println("HelloWorld"+x); } }

2 loop statement-
while while loop statement format:
initialization expression ①
while (boolean expression ②) {
loop body ③
step expression ④}
execution flow execution sequence: ①②③④> ②③④> ②③④ ... ②Unsatisfied.
① Responsible for completing the initialization of loop variables.
②Responsible for judging whether the cycle condition is met, if not, jump out of the cycle.
③ The specific execution statement.
④ After the circulation, the change of the circulation variable.
Insert picture description here
while loop outputs HelloWorld 10 times

public static void main(String[] args) {
 //while循环实现打印10次HelloWorld
  //定义初始化变量
   int i = 1; 
  //循环条件<=10 
  while(i<=10){ 
  System.out.println("HelloWorld");
   //步进
    i++; }
     }

while loop calculates the sum between 1-100

public static void main(String[] args) {
 //使用while循环实现 
 //定义一个变量,记录累加求和 int sum = 0;
  //定义初始化表达式 
  int i = 1; 
  //使用while循环让初始化表达式的值变化 
  while(i<=100){ 
  //累加求和 sum += i ;
   / /步进表达式改变变量的值 
   i++;
   }
   //打印求和的变量 
   System.out.println("1‐100的和是:"+sum); 
   }

3 Loop statement –do… while
do… while loop format
Initialization expression ①
do {Loop body ③Step
expression ④
} while (Boolean expression ②);
Execution flow execution sequence: ①③④> ②③④> ②③④ ... ②Unsatisfied .
① Responsible for completing the initialization of loop variables.
②Responsible for judging whether the cycle condition is met, if not, jump out of the cycle.
③The specific execution statement
④After the loop, the change of the loop variable is
Insert picture description here
output 10 times HelloWorld

public static void main(String[] args) { 
int x=1;
 do {System.out.println("HelloWorld"); 
 x++;
  }while(x<=10); 
 }
Posted 28 original articles · Like1 · Visits1710

Guess you like

Origin blog.csdn.net/qq_45870494/article/details/103333124