1031 Learning the three cyclic structures of daily newspapers 1031 Learning the three cyclic structures
of daily newspapers
Review
Branch structure
if structure
grammar
if (condition) { execute the content here when established }
// Two-way
if (condition) { execute the content here when established ; } else { execute the content here when the condition is not established; }
// Multi-way branch
if (condition) { execute the content here; } else if (condition n) { statement to be executed when condition n is established; } else { if the condition is not established, execute the content here; }
Copy code
switch structure
grammar
switch (variable) { case value 1: when the variable is equal to the value 1, execute the code here; break; case value n: when the variable is equal to the value n, execute the code here; break; default: when the variable is not equal to the above In case of all values, it will enter this branch; break; }
Copy code
while loop
Loop structure
Function
Repeat the implementation of some elements of
grammar
while (condition) { thing that needs to be repeated; }
Copy code
requirements
Print three star flowers
Syntax 2
while (condition) { if the condition is established, the content here will be executed; }
Will come here if the conditions are not met
Copy code
example
Output ordered numbers
int i = 0;
while (i < 6) {
System.out.println("!" + i);
i++;
}
System.out.println(“退出循环”);
Copy code
Flow chart
Insert picture description here
Three elements of loop
condition variable
Conditional judgment
Make appropriate modifications to the condition variable in the loop
example
public class Draw {
public static void main(String[] args) {
// 条件变量
int i = 0;
// 条件判断:决定循环能否进入,会对条件变量的数据进行判断
while (i < 6) {
System.out.println("!" + i);
// 条件变量一定发生合适的变化
i++;
}
System.out.println("退出循环");
}
}