1. While Loop statement
The While Loop statement also becomes a conditional judgment statement, which loops in a condition that controls whether the statement continues to execute repeatedly. The syntax format is as follows:
While(conditional expression)
{
EXECUTE statement
}
When the return value of the conditional expression is true, the statement in {} is executed, and when the statement in {} is executed, the return value of the conditional expression is re-judged until the result returned by the expression is false, exiting the loop.
Example: seeking the 1+2+3+...+100 and
Package Com.hanqi;
public class Xunhuan {
public static void Main (string[] args) {
int i=1; //Counter
int sum=0; //Calculation results
while (i<=100)//While loop
{
sum + = i;
i++;
}
System.out.println ("1 to 100 and is:" + sum);
Exercise 1: Calculate factorial n! =n* (n-1) *...*1
int n=0;
int k=1;
while (n<100)
{
n=n+1;
K=k*n;
if (k>200000000) //early end loop
{
Break
}
SYSTEM.OUT.PRINTLN (n+ "! =" +k);
}
Exercise 2: Paper thickness of 0.08mm, origami how many times to reach 8848m
Double thickness=0.08/1000;
int times=0;
while (thickness<8848)
{
thickness*=2;
times++;
System.out.println ("Origami" +times+ "times" + "Thickness:" +thickness+ "M");
}
System.out.println ("Origami" +times+ "times" + "thickness up to 8848m");
2. Do and loop statements
Similar to the While Loop statement, the difference between them is that the while Loop statement is the first to determine whether the condition is the execution loop body, while the Do While loop statement first executes a loop, and then determine whether the condition is true.
The Do While loop statement has a semicolon ";" in the end, with the syntax in the following format:
Do
{
EXECUTE statement
}
while (conditional expression)
3. For Loop statement
A For loop can be used to repeatedly execute a statement, knowing that a condition is satisfied. The format is as follows:
for (expression 1; expression 2; expression 3)
{
Statement sequence
}
Example: 1 to 10 odd and (3 not included)
int k=0;
for (int m=1;m<10;m++)
{
if (m==3)
{
Continue //Skip back code to continue looping
}
K=k+m;
System.out.println ("m=" +m+ "\tk=" +k);
}
Process Control (ii)---loop statement (25TH,FEB)