Loop Structure
when type: when P When the condition is set (T), repeat A until p is " false cycle is not stopped .
until type: Execute First A, then Judge p, if it is T, then execute A, so repeat until p to F.
1.While Cycle
the basic format and flowchart of the while loop:
while ( boolean expression ) {
circulation body;
}
The while loop is detailed:
at the beginning of the loop, the value of the boolean expression is computed once, and if the condition is true, the loop body is executed. And for each additional cycle, it will be recalculated before the start.
there should be statements that make loops tend to end, or an infinite loop-a " dead " loop
While loop code:
/*** Test While Loop*/ Packagecom.oliver.test; Public classTestwhile { Public Static voidMain (string[] args) {//Print 1 to intA = 1; while(a<=100) {System.out.println (a); A++; } //ask for 1 to 100 of the and intb = 1 ; intsum = 0; while(b<=100) {sum+=b; b++; } System.out.println (sum); }}
2.do-while Cycle
do-while The basic format of the loop:
do {
circulation body;
} while ( Boolean expression );
Do-while Cycle instructions and flowchart:
1. perform the loop body first
2. post-judgment Boolean expression
3. The loop body is executed at least once
Do-while Loop Code:
Package com.oliver.test; Public class Dowhile { publicstaticvoid main (string[] args) { int i = 1; Do { = i+1; I++ ; } while (i<=10); System.out.println ("I's value is" +i);} }
The difference between while and do-while
while first judgment, after execution
Dowhile Execute first, then Judge
3. For Loop
A for Loop statement is a common structure that supports iterations and is the most efficient and flexible loop structure
Grammatical form
For ( an initial expression ; Boolean expression ; Step in ) {
circulation body;
}
the For loop is initialized before the first iteration. It then tests the condition and, at each iteration, carries out some form of "stepping".
- Initialization section sets the initial value of the loop variable
- The conditional judgment part is any Boolean expression
- Iterative factors control the increment and decrease of cyclic variables
"Attention" The For Loop executes the program section after the condition test, and then Execution Step in .
For Loop code:
/*** * package com.oliver.test; Public class testfor { publicstaticvoid main (string[] args) { for (int i=1;i<=10;i++) { System.out.println (i); }}}
"Java" JAVA_14 loop structure