1. There are 3 types of loop control statements in Java, namely While,do ... while and for loops.
2. While loop in the form:
while (boolean-expression)
{
Code to be executed
}
Class whiletest{ public static void main (string[] args) {int a = 2; // loop variable int sum = 0; // while (A <= 100 += A; A + = 2; } System.out.println (sum); }}
3. Do...while cycle, the new type is:
Do
{
Code to be executed
}
while (Boolean expression);
Public classdowhiletest{ Public Static voidMain (string[] args) {intA = 2;//Loop Variable intsum = 0;//results after the storage is added Do{sum+=A; A+ = 2; } while(A <= 100); SYSTEM.OUT.PRINTLN (sum); intb = 2; while(b < 1) {System.out.println ("Hello World"); } Do{System.out.println ("Welcome"); } while(b < 1); }}
4. The difference between while and Do...while: if the first judgement of a Boolean expression is false, then the while loop does not execute once, and the Do...while loop executes once.
If the Boolean expression evaluates to True for the first time, then the while loop is equivalent to the Do...while loop.
5. For loop (one of the most used loops), in the form:
for (variable initialization; condition judgment; stepping)
{
Code to be executed
}
The execution process for the FOR loop:
1) perform variable initialization.
2) perform the condition judgment. If the condition determines that the result is false, exit the for loop, start executing the code after the loop, or, if the condition is true, execute the code inside the For loop.
3) perform stepping.
4) Repeat step 2.
Public classfortest{ Public Static voidMain (string[] args) {intsum = 0;//results after the storage is added for(inti = 2; I <= 100; i + = 2) {sum+=i; } System.out.println (sum); intsum2 = 0;//results after the storage is added for(inti = 100; I >= 1; i--) {sum2+=i; } System.out.println (SUM2); }}
Loop control Statements