当前位置:网站首页>Circular structure and circular keywords
Circular structure and circular keywords
2022-06-25 21:22:00 【vancomycin】
Loop structure
1. The characteristics of circulation structure
Any cycle requires four essential conditions
1. Counter initialization
2. The loop condition
3. The loop body
4. The counter changes
2. while loop
while word : When ……
while ( The loop condition ) {
Cyclic operation
}
package com.qfedu.test1;
import java.util.Scanner;
/**
* demand : The teacher checks whether Zhao Si's learning task is qualified every day ,
* If it's not qualified , I'm going to continue .
* The daily learning task assigned by the teacher to Zhao Si is :
* Read the textbook in the morning , Study the theory part , Programming in the afternoon , Master the code part
*
*/
public class Test3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Is your academic record qualified ?y/n");
String answer = input.next();
while(answer.equals("n")) {
System.out.println(" Read the textbook in the morning , Study the theory part ");
System.out.println(" Programming in the afternoon , Master the code part ");
System.out.println(" Is your academic record qualified ?y/n");
answer = input.next();
}
System.out.println(" congratulations , To complete the task !");
}
}3. do-while loop
while and do-while The difference between
do-while loop Execute before judge Whether or not the conditions are valid At least once
while loop Judge before you execute Conditions not established Not once
package com.qfedu.test2;
import java.util.Scanner;
/**
* difference :while and do-while The difference between
* do-while loop Execute before judge Whether or not the conditions are valid At least once
* while loop Judge before you execute Conditions not established Not once
*
* demand : After a few days of study , The teacher gave Zhaosi a test question ,
* Let him write a program on the computer first , Then the teacher checks whether it is qualified . If it's not qualified , Continue to write ……
*
*/
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String answer;
do {
System.out.println(" Write code , On board test ……");
System.out.println(" Please enter whether it is qualified ?y/n");
answer = input.next();
}while(answer.equals("n"));
System.out.println(" Congratulations on completing the task !");
}
}
package com.qfedu.test2;
/**
* Use do-while Loop printing N Study hard all the time
*
*/
public class Test2 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(" study hard " + i);
i++;
}while(i <= 1000);
}
}
4.for loop
word in order to ……
The first round
1. Counter initialization The first to perform And only once
2. Judge the condition
3. The loop body
4. The counter changes
The second round
Execute directly from the judgment condition
The number of cycles is fixed , In certain cases ,for Cycle ratio while and do-while Simple writing
package com.qfedu.test3;
/**
* for loop
* word in order to ……
* for Loop execution process
* The first round
* 1. Counter initialization The first to perform And only once
* 2. Judge the condition
* 3. The loop body
* 4. The counter changes
* The second round
* Execute directly from the judgment condition
*
* The number of cycles is fixed , In certain cases ,for Cycle ratio while and do-while Simple writing
*
*/
public class Test1 {
public static void main(String[] args) {
for(int i = 0;i < 15;i++) {
System.out.println(" study hard " + i);
}
System.out.println(" Program end ");
}
}
package com.qfedu.test3;
import java.util.Scanner;
/**
* Loop in a student's final exam 5 Course results , And calculate the average score
*
*/
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter a name ");
String name = input.next();
double sum = 0;
for(int i = 1;i <= 5;i++) {
System.out.println(" Please enter the first "+ i +" Results ");
double score = input.nextDouble();
sum += score;
}
System.out.println(" The total score is :" + sum);
System.out.println(name + " The average score of the students is :" + sum / 5);
System.out.println(" Program end ");
}
}4.1 for Circulation FAQs
for Circulation FAQs 1 Counter not initialized
package com.qfedu.test4;
/**
* for Circulation FAQs 1 Counter not initialized
*/
public class Test2 {
public static void main(String[] args) {
int i = 0;
for(;i < 10;i++) {
System.out.println(i);
}
}
}for Circulation FAQs 2 Lack of judgment conditions
package com.qfedu.test4;
/**
* for Circulation FAQs 2 Lack of judgment conditions
*
*/
public class Test3 {
public static void main(String[] args) {
for(int i = 0;;i ++) {
System.out.println(i);
}
// System.out.println(" Program end "); Because of the above for The loop lacks judgment conditions So the code cannot be executed here
}
}for Circulation FAQs 3 Missing counter change
package com.qfedu.test4;
/**
* for Circulation FAQs 3 Missing counter change
*
*/
public class Test4 {
public static void main(String[] args) {
for(int i = 0;i < 10;) { // Missing counter change
System.out.println(i);
}
}
}
for Circulation FAQs 4 Missing counter initialization 、 Judge the condition 、 The counter changes
package com.qfedu.test4;
/**
* for Circulation FAQs 4 Missing counter initialization 、 Judge the condition 、 The counter changes
*
*/
public class Test5 {
public static void main(String[] args) {
for(;;) {
System.out.println("hello world");
}
}
}
5. Cycle comparison and summary
difference
difference 1: grammar

difference 2: Execution order while loop : First judge , Re execution do-while loop : Execute first , To determine for loop : First judge , Re execution
difference 3: Applicable scenario
The number of cycles is determined , Generally selected for loop The number of cycles is uncertain , Generally selected while or do-while loop
6.break keyword
Applicable scenario :
1.switch in To jump out of switch structure
2.3 In a cycle Represents an interrupt loop structure The number of cycles not completed No more execution
Usually used in conjunction with branch structures
package com.qfedu.test5;
/**
* Use do-while loop break Run exit
*
*/
public class Test3 {
public static void main(String[] args) {
int i = 1;
do {
if(i == 3) {
System.out.println(" Too tired , sign out ");
break;
}
System.out.println(" Running page "+ i +" circle ");
i++;
}while(i <= 10);
System.out.println(" Program end ");
}
}
practice
package com.qfedu.test5;
import java.util.Scanner;
/**
* Use circular entry 5 Results If you enter a negative number Stop typing And prompt
*
*/
public class Test4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sum = 0;
boolean flag = true;
for(int i = 1;i <= 5;i++) {
System.out.println(" Please enter the first "+ i +" Results ");
int score = input.nextInt();
if(score < 0) {
flag = false;
System.out.println(" Score entry error , Please re-enter ");
break;
}
sum += score;
}
if(flag) { // flag == true
System.out.println(" The average score is " + sum / 5);
}else {
System.out.println(" Incorrect score entry , No more averaging ");
}
}
}
7. continue keyword
continue continue
Applicable scenario : It can only be used in a cycle , Jump out of this cycle , Continue with next cycle
package com.qfedu.test6;
import java.util.Scanner;
/**
* continue continue
* Applicable scenario : It can only be used in a cycle , Jump out of this cycle , Continue with next cycle
* Cycle input Java Students' grades in class , The statistical score is greater than or equal to 80 The proportion of students who get a score
*
*/
public class Test2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Please enter the number of people ");
int num = input.nextInt();
double count = 0;
for(int i = 1;i <= num;i++) {
System.out.println(" Please enter the first "+ i +" Personal achievements ");
int score = input.nextInt();
if(score <= 80) {
continue;
}
count ++;
}
System.out.println(" Greater than 80 The total number of points is :" + count);
System.out.println(" Greater than 80 The percentage of sub population is :" + count / num * 100 + "%");
}
}8.break and continue The difference between
Use occasion break be used for switch In structure and cyclic structure continue Used in cyclic structures
effect ( In cyclic structure ) break Statement terminates a loop , The program jumps to the next statement outside the loop block continue Jump out of this cycle , Enter next cycle The same is true of the double cycle
边栏推荐
- JS__ Inheritance mode, namespace, object enumeration__ Duyi
- The robotframework executes CMD commands and bat scripts
- Basic knowledge of software engineering required for soft test
- Solve the parameter problem that laravels cannot receive wechat callback
- The robot framework calls the JS interface and gets the return value
- Lesson 3 urllib
- Code program related problems troubleshooting directory
- Free your hands and automatically brush Tiktok
- Molecular dynamics - basic characteristics of molecular force field
- Docker failed to remotely access 3306 after installing MySQL
猜你喜欢
![[nailing scenario capability package] video conference (official conference system)](/img/ec/c2f342a54ab69d8b834a8a1c8f8a01.jpg)
[nailing scenario capability package] video conference (official conference system)

1.1-mq visual client preliminary practice

01 network basics
![[nail scenario capability package] hospital visitor verification](/img/0e/43433ca5586c48d01708e5fa39a808.jpg)
[nail scenario capability package] hospital visitor verification

Local Yum source production

Command 'GCC' failed with exit status 1 when PIP install mysqlclient

Getting started and using postman

CANoe. Diva operation guide TP layer test

Docker failed to remotely access 3306 after installing MySQL

Insert and update each database
随机推荐
Openocd adds third-party device support: ht32f52352 Cortex-M0+
Cross project measurement is a good helper for CTOs and PMOS
IPtables
"Developer theory" multi system integrated development - rapid nailing of enterprise owned systems
Desktop network error display red ×, Component failed to start
JS__ Inheritance mode, namespace, object enumeration__ Duyi
Lesson 4 beautifulsoup
Illustrated with pictures and texts, 700 pages of machine learning notes are popular! Worth learning
Dbeaver offline installation driver
Command 'GCC' failed with exit status 1 when PIP install mysqlclient
Differences between modems and routers (powercert animated videos)
[machine learning] machine learning from zero to mastery -- teach you to recognize handwritten digits using KNN
PHP compressed file
04 disk space management
How to write an infinite loop
Data query of server SQL. The most important chapter in database learning
Lesson 3 urllib
Xshell mouse configuration
What is machine learning? (Fundamentals)
What is a subnet mask? (Powercert animated videos)
