Loop Structure and loop structure
Execution of cyclic statements
For Loop usage, such as printing an odd number between 1 and 10
Public class testFor {public static void main (String args []) {System. out. println ("loop start"); for (int I = 1; I <= 10; I + = 2) {System. out. println (I);} System. out. println ("loop end ");}}
Process: first declare I as 0 and judge whether the I value is smaller than 10. If yes, print out the I value and then add 2 to the I value, determine whether the I value is less than 10. If yes, print out the I value... When the I value is greater than 10, the loop jumps out.
How to Use the While LOOP, such as printing an odd number between 1 and 10
public class testWhile{ public static void main(String args[]){ int i=1; while(i <= 10){ System.out.println(i); i += 2; } }}
Process: first declare I as 0, judge whether the while Boolean expression is true, if yes, execute the loop body, add 2 to the I value, and then judge whether the Boolean expression is true, loop until the result of the Boolean expression is false.
C language: write programs separately using the Loop Structure
# Include <stdio. h>
Void main ()
{
Int n = 1;
Float temp;
Float sum = 0;
Do
{
Temp = (float) 1/(2 * N-1 );
If (n % 2 = 1)
Sum + = temp;
Else
Sum-= temp;
N ++;
} While (temp & gt; = 0.000001 );
Printf ("Pi = % f \ n", sum * 4 );
}
C language programming-cyclic structure
Your program calculates the sum between 1 and 50, and the cyclic variable is a, because the value of a has been changing. Generally, the third expression of the for statement contains a cyclic variable, the second expression is a circular condition, that is, a <= 50 is a circular condition, and the statement sum = sum + a after for is a circular body, loop variables are the basis for loop variables, so there is no way to loop. The cycle condition controls the number of cycles. The loop body is the purpose, that is, the part you require. The program without a loop variable is an endless loop. The value of a is always 1 and the loop cannot be ended.