Loop Structure and loop structure

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.