YouTip LogoYouTip

C Loops

# C Loops Sometimes, we may need to execute the same block of code multiple times. In general, statements are executed sequentially: the first statement in a function is executed first, followed by the second statement, and so on. Programming languages provide various control structures that allow for more complex execution paths. A loop statement allows us to execute a statement or group of statements multiple times. The following is the general form of a loop statement in most programming languages: ![Image 2: Loop Structure](#) ## Loop Types The C language provides the following types of loops. Click on the links to view details for each type. | Loop Type | Description | | --- | --- | | (#) | Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. | | (#) | Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable. | | [do...while loop](#) | Similar to the while statement, except that it tests the condition at the end of the loop body. | | (#) | You can use one or more loops inside any other while, for, or do..while loop. | More: (#) ## Loop Control Statements Loop control statements change the execution sequence from its normal sequence. When the execution sequence is changed, you can execute jumps in the code. C provides the following loop control statements. Click on the links to view details for each statement. | Control Statement | Description | | --- | --- | | (#) | Terminates the **loop** or **switch** statement and transfers execution to the statement immediately following the loop or switch. | | (#) | Causes the loop to skip the remainder of its body and immediately retest its condition before reiterating. | | (#) | Transfers control to the labeled statement. Though it is not recommended to use the goto statement in your program. | ## Infinite Loop A loop becomes an infinite loop if the condition never becomes false. The **for** loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an infinite loop by leaving the conditional expression empty. ## Example ```c #include int main() { for( ; ; ) { printf("This loop will run forever.n"); } return 0; } When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C programmers more commonly use the `for(;;)` structure to denote an infinite loop. **NOTE:** You can terminate an infinite loop by pressing Ctrl + C keys. [](#)(#) (#)[](#) ## 6 Notes - Write a Note 1. #0 Zhang Mingzhe 164***8264@qq.com [](#)247 Using while and for to print the sum of all odd numbers and even numbers within 1~100 respectively: **Using while:** ```c #include int main() { int sum=0; int num=1; int sum2=0; int num2=2; while(num<100) { sum=sum+num; num=num+2; } printf("Sum of odd numbers: %dn",sum); while(num2<=100) { sum2=sum2+num2; num2=num2+2; } printf("Sum of even numbers: %dn",sum2); } **Using for:** ```c #include int main() { int sum=0; int sum2=0; int num,num2; for(num=1;num<100;num=num+2) { sum=sum+num; } printf("Sum of odd numbers: %dn",sum); for(num2=2;num2<=100;num2=num2+2) { sum2=sum2+num2; } printf("Sum of even numbers: %dn",sum2); } (javascript:;)Zhang Mingzhe 164***8264@qq.com 9 years ago (2017-09-03) 2. #0 kevintcl 299***2513@qq.com [](#)127 Using do while to calculate square root: ```c #include double DoSqrt(double z) { double a=1; double b=0; double c=0; do { if(b*b0.000001); return (b+c)/2; } int main() { double x, y; printf("Please enter a number:"); scanf("%lf", &x); if(x10); return 0; } (javascript:;)kevintcl 299***2513@qq.com 9 years ago (2017-10-21) 3. #0 That Autumn 158***07539@163.com [](#)130 Let's look at a simple example, finding all prime numbers within 100. ```c #include #include int main() { int i,j; printf("Prime numbers within 100:n"); for(i=2;i<100;i++) { for(j=2;jsqrt(i)) { printf("%d,t",i); } } } In the first loop, `i` iterates through all numbers from 2 to 100. The second loop checks whether numbers from 2 to `sqrt(n)` can evenly divide `i`. If it can be divided evenly, it is not a prime number; conversely, if it cannot be divided evenly, it is a prime number. ```c int fun(int n) { for(i=2;i<=sqrt(p);i++) { if(x%i==0) { return 0; // Not a prime number } else { return 1; // Is a prime number } } } This is the key code for finding prime numbers. Beginners should memorize it thoroughly. (javascript:;)That Autumn 158
← React InstallReact Tutorial β†’