Sixth chapter
For syntax:
for (expression ①; expression ②; expression ③) {
④ Loop operation
}
Expression meaning:
Expression 1: An assignment statement, which is used to assign an initial value to a loop variable, for example: int i = 1;
Expression 2: Loop condition, a relational expression, which determines when to exit a loop such as: I < 5;
Expression 3: Variable Iteration Example: i++
Three expressions can be omitted, but semicolons cannot save!!!!!
Execution process:
1. Expression 1
2. Judge Expression 2, if true, perform a loop operation, otherwise exit the loop, the third step does not execute
3. After execution of the loop operation, execute expression 3 (iteration part), change the value of the loop variable
4. Repeat steps, or both, in sequence until you exit the loop
Expression 2 satisfies the condition
if (expression ①; expression ②;④ loop operation) {
Expression ③
}
Execution process: ①---->②---->③---->④
--------------------------------------------------------------------------
Precautions:
for (; i<10;i++) {
System.out.println ("This is" +i);
}
Compile error:
Variable i is not initialized
--------------------------------------------------------------------------
for (int i=0;; i++) {
System.out.println ("This is" +i);
}
Compiled correctly, but missing
Cyclic conditions, resulting in a dead loop
--------------------------------------------------------------------------
for (int i=0;i<10;) {
System.out.println ("This is" +i);
}
i++;
Compilation passes, but the value of the loop variable does not change,
Cause a dead loop
Omit expression 3, in the loop body should try to change
Change the value of the loop variable to end the loop
--------------------------------------------------------------------------
for (;;) {
System.out.println ("This is the test");
}
The expression of the province slightly, unconditional judgment, the cyclic variable no change,
You should try to end the loop in the loop, otherwise it will cause a dead loop.
--------------------------------------------------------------------------
Break is the end of the entire loop body, continue is the end of a single cycle
Say:
while (x + + < 10) {
if (x = = 3) {
Break
}
System.out.println ("Value:" + x);
}
As a result, Output 1 2 exits the entire while loop.
But if you use continue
while (x + + < 10) {
if (x = = 3) {
Continue
}
System.out.println ("Value:" + x);
}
The result is: 1 2 4 5 6 7 8 9 10 Visible He just does not output 3 because he ended the cycle
When the loop executes to the break statement, it exits the entire loop and then executes the out-of-loop statement to change the program flow.
When the Loop statement executes to continue, the next cycle is restarted when the secondary loop ends. If this is the last cycle, then this is the same continue as the break effect.
What are the cyclic structures that have been learned so far?
Syntax differences:
While |do-while |for
While loop: |do{|for (initialization; condition; iteration) {
while (< conditions >) {|//|//loop body
Loop Body |} while (< conditions >); |}
| |
Issues that require multiple repetitions of one or more tasks consider using loops to resolve
No matter which loop structure, there are 4 essential parts: The initial part, the cycle condition, the loop body, the iteration part
Execution order:
While loop: First judge, then execute
Do-while Cycle: Execute first, then judge
For loop: First judge, then execute
Applicable situation:
When the number of cycles is determined, the for loop is usually selected
When the number of cycles is uncertain, a while or a do-while loop is usually selected
For case:
/**
* Calculates 1 to 100 of the and
*/
public class Summation {
public static void Main (string[] args) {
int sum = 0; Declare the sum of the variables that are stored in the calculation and assign the initial value to 0
for (int i=1;i<=100;i++)
Sum +=i;//is equivalent to statement Sum=sum+score
System.out.println ("sum=" +sum);
}
}
/**
* Use for loop
* Enter 5 students ' homework to check the student's total and average score
*/
Import Java.util.Scanner;
public class Averagescores {
public static void Main (string[] args) {
int score; Declare a variable, named score, to receive students ' performance values
int sum= 0; Declare a variable, named Sum, to store students ' scores and
Double avg = 0.0; Declare a variable of type double to receive the average score of the calculated student
Scanner sc = new Scanner (system.in); Get keyboard input
System.out.println ("Please enter the student's name:");
String name = Sc.next (); Assigns the obtained input to a string type name variable
int i= 0; Declares a variable of type int, assigning an initial value of 0
for (; i<5;i++) {//Loop 5 entry Results
System.out.println ("Please enter 5 lessons of the first" + (i+1) + "The results of the door:");
Score = Sc.nextint (); Entry Score
sum + = score; Equivalent to statement sum=sum+score;
System.out.println (name+ "before" + (i+1) + "The results of the homework and is:" +sum);
}
System.out.println (name+ "The overall grade is:" +sum);
AVG = SUM/5; Calculate Average score
System.out.println (name+ "The average score is:" +avg);
}
Variable is Chinese character
/**
* Use for loop
* Enter 5 students ' homework to check the student's total and average score
*/
Import Java.util.Scanner;
Public class asks students for average results {
public static void Main (string[] args) {
int single section results;
int total = 0;
Double mean score = 0;
Scanner keyboard input = new Scanner (system.in);
System.out.println ("Please enter the student's name:");
String Student name = keyboard input. Next ();
int i= 0;
for (; i<5;i++) {
System.out.println ("Please enter 5 lessons of the first" + (i+1) + "The results of the door:");
Single score = keyboard input. Nextint ();
Total = + single-branch results;
SYSTEM.OUT.PRINTLN (student name + "before" + (i+1) + "The results of the homework and is:" + overall score);
}
SYSTEM.OUT.PRINTLN (student name + "The overall grade is:" + total);
Average = total score/5;
SYSTEM.OUT.PRINTLN (student name + "The average score is:" + average score);
}
}
Java Chapter Sixth