Java basis of Chapter VI (cyclic structure of the titanium)

A, for circulation

grammar:

for (Expression 1; 2 Expression; Expression 3) {
// loop body
}
1. Expression 1 is an assignment statement, the initialization part of the cyclic structure, the loop variable is assigned an initial value, for example: int i = 0;
2. expression 2 conditional statements, loop condition of the cyclic structure, for example: I <100
3. expression 3 assignments, usually + or - operator. Iterative loop structure portion, typically used to modify the loop variable value, for example: i ++

Example:

package cn.ytzl.test;

import java.util.Scanner;

the Test class {public
public static void main (String [] args) {
Double Score; // score for each course
double sum = 0; // Achievements and
double avg = 0.0; // average
Scanner input = new Scanner ( the System.in);
of System.out.print ( "enter a student name:");
String name = input.next ();
for (int I = 0; I <5; I ++) {// 5 cycles, entry 5 achievement course
System.out.print ( "Please enter the five homework first" + (i + 1) + " course of performance");
score = input.nextDouble (); // entry score
sum = sum + score; // score and calculating
}
AVG = SUM /. 5; // calculate average
System.out.println (name + "average is:" + AVG);
}
}


In this example, four cycles are as follows regardless
1. "int i = 0;" is the initial part, the statement loop variable i, used to record the number of cycles
2. "i <5" is the loop condition
3. "i ++ "is an iteration section, updating the value of the loop variable
4. the loop body is the" input result, updates the value of the loop variable "
whole cycle: firstly performs initialization section, i.e., i = 0; then the loop condition is determined, if true, is performed a loop; i ++ implementation of the iterative part after the end of the loop; the loop condition in determining if the throw is true, then continue the loop, the iteration part ... and so on, know the loop condition is false, exit the loop

Second, the jump statement

1, break statement: used in a loop, you can jump out of the current cycle. As long as the cycle encountered break, this cycle will end

2, continue statement: used in a loop, the loop will continue to skip across the cycle into the next cycle.

Third, the loop structure Summary:

No matter what kind of looping structure, there are four essential regardless: the initial part of the loop condition, loop, iterative part, the lack of any likely walked into a serious error

Different syntax
while loop as follows
while (<condition>) {
// loop body
}
do-while loop as follows:
do {
// loop body
} while (<condition>);
for loop as follows:
for (initialization; condition; iterative) {
// loop body
}
a different order of execution:
1.while loop structure: first, a conditional judgment, then execution cycle thereof. If the condition is not satisfied, the loop is exited
2.do-while loop structure: the first iteration of the loop, then Jining condition determination, the loop body at least once
3.for loop structure: Execute the initial portion, and then makes a conditional judgment, then the execution cycle thereof, Finally, the iterative calculation section. If the condition is not satisfied, out of the loop

Fourth, the chapter concludes

Syntax 1.for cyclic structure as follows
for (1 Expression; Expression 2; Expression 3) {
// loop body
}
Expression 1: the initial part of the cyclic structure, a cyclic variable assignment
expression 2: cyclic cyclic structure condition
expression 3: an iterative loop structure portion, typically used to modify the value of the variable cycle
2. in cycle can use the process control program statements break and continue to
break; statement is used to terminate a loop, the program jumps to the next statement vitro cycle
continue statement out for this cycle, the next cycle into the statements

Guess you like

Origin www.cnblogs.com/aryl/p/12141741.html