The Java for loop is the manual control of the number of cycles for some operations. The simplest, such as printing numbers.
Example: Iterating through an array
1234 |
int
[] a =
new
int
[]{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
};
for
(
int
iloop : a){
System.out.println(iloop);
}
|
The 2.while structure cycle is a type loop (when type loop), which is typically used for situations where the number of loops is not known. The maintenance loop is a conditional expression, the condition is set up to execute the loop body, the condition does not set up the exit cycle.
650) this.width=650; "Src=" http://e.hiphotos.baidu.com/baike/s%3D250/sign=dd10d818a044ad342abf8082e0a30c08/ 9825bc315c6034a812ecc274ca1349540923763c.jpg "style=" border:0px;margin:0px auto;width:250px;height:86px;/>
while (conditional expression)
{
Circulation body;
}
The conditional expression is judged before each execution of the loop body.
The expression here is the condition that the loop can continue, and the statement is the loop body. As long as the expression is true, the loop in-body statement is executed. Otherwise terminates the loop and executes the loop out-of-body statement.
# include H
Main ()
{
int u,v,temp;
printf ("Please enter two non-negative integers: \ n");
scanf ("% d% d",& u,&v);
while (v!=0)
{
temp=u% v;/* Loop Body statement */
U=v; /* Loop BODY statement */
V=temp; /* Loop BODY statement */
}
printf ("Greatest common divisor is:%d\n", u);
}
Compile and execute the results, the screen displays:
Please enter two non-negative integers:
If you enter from the keyboard: 150 35 and enter, the screen displays:
Greatest common divisor is: 5
In the program, the while Loop has three statements in its body, enclosed in curly braces, to represent a block of statements. When the expression v!=0 is true, the program loops through three statements in the loop body until v!=0 is false, terminates the loop, executes the printf () statement, and displays the greatest common divisor on the screen.
Do While
The DO...WHILE statement structure is until the type loop (until type loop) and is also used for situations where the number of loops is not known. The difference between the do...while and the while is that the do...while structure is the condition of the re-judging of the cyclic body after execution.
650) this.width=650; "Src=" http://g.hiphotos.baidu.com/baike/s%3D250/sign=024d4cfe5ab5c9ea66f304e6e53bb622/ B8389b504fc2d562b3e3aa18e61190ef77c66c5f.jpg "style=" border:0px;margin:0px auto;width:250px;height:72px;/>
Do
{
Circulation body;
}while (conditional expression);
Each time the loop body is executed, the DO...WHILE structure must judge the conditional expression.
After the program enters the Do and loop, the loop body statement is executed first, and then the true and false expression is judged
The next loop, or false to terminate the loop. The characteristic of this loop statement is that the expression is also executed once in the loop body statement.
In general, the for and while loops are tested at the top of the loop termination condition, and the Do While loop is then tested at the bottom so that the loop body executes at least once.
A menu program can be written using the features of the Do and loop.
#include <stdio.h>
Main ()
{
Char ch;
printf ("1. Input record \ n");
printf ("2. Delete record \ n");
printf ("3. Display record \ n");
printf ("Please select: \ n");
Do
{
Ch=getchar ();
Switch (CH)
{
Case ' 1 ':p rintf ("input record \ n");
Break
Case ' 2 ':p rintf ("delete record \ n");
Break
Case ' 3 ':p rintf ("Display record \ n");
Break
default:printf ("Wrong choice!") \ n ");
}
}while (ch!= ' 1 ' &&ch!= ' 2 ' &&ch!= ' 3 ');
}
The difference between the while and do while
Suppose the conditional expression is not tenable at first.
The loop body of the while structure does not run again.
The loop body of the do...while structure runs over again.
JAVA for Loop