Loop is one of the three basic structure of C language, in many problems need to use the loop control, such as 1~100 can not be divisible by 7 of the sum of the number, if not to use the loop, we can only 1~100 can not be divisible by 7 of the number to find out, and then found their sum, but if the use of loops, You can let the program statements from 1 to start the loop, as long as the number is not divisible by 7 will be output and calculate the cumulative sum, after the last loop operation, you can get the final result, this time using the Loop statement is necessary.
1, circular statement structure Looping statements are divided into four kinds: while,do...while,for,goto (goto statement is not recommended, this article does not do specific). The statement structure of each statement is not the same, but can be used to meet the constraints (that is, the expression) loop, if you meet the constraints of the loop, the loop will continue, that is, the dead loop (the eternal loop, such as while (1)), but the program will not error, but it will consume a lot of memory, Therefore, each loop statement must have a condition for exiting the loop. (1) While Statement while statement is used to implement the "when" loop, that is, the constraint is preceded by the loop variable is initialized before the while statement, the basic structure is: while (constraints, such as i<100) { Loop body such as:sum+=i; i++;} Loop body If you want to use "{}" for more than one sentence, the range of the loop includes only the first sentence after the while (). (2) The Do...while statement do...while is used to denote the "until" loop, and the difference between the while is that the do...while of the constraint after the execution of the statement before, that is, whether or not to meet the cyclic condition requirements, do ... The While statement executes at least once, initializing the loop variable before the loop statement, with the basic structure: do{ loop body } while (restriction conditions such as i<100); The While loop, while () has a ";") (3) For statement for statements also belong to the "when" loop, and while statements can be interchanged, but the while statement is easier to use and is the most used statement in the loop, The basic structure of the statement is: for (variable initialization; restriction 1; Execute statement 2) { EXECUTE statement 1}for loop can re-loop the body in the initialization of the variable, The execution order of the statements is: Initialize the variables first?? Judging the restrictions?? Execute statement 1?? Execute statement 2 2, loop instance below you can use several instances to apply the loop statement and distinguish between the similarities and differences in the application of the circular statement: instance one: Seeking the accumulation and a of 1~100. Using the While loop
#include <stdio.h>
int main (int argc,constchar * argv[]) {
int i=1,sum=0;
while (i<=100) {
Sum=sum+i;
i++;
}
printf ("1+2+3+...+100=%d\n", sum);
}
B. Using the Do...while loop
#include <stdio.h>
int main (int argc,constchar * argv[]) {
int i=1,sum=0;
Do
{
Sum=sum+i;
i++;
}
while (i<=100);
printf ("1+2+3+...+100=%d\n", sum);
}
C. Using a For loop
#include <stdio.h>
int main (int argc,constchar * argv[]) {
int sum=0;
for (int i=1;i<=100;i++)
Sum=sum+i;
printf ("1+2+3+...+100=%d\n", sum);
}
Three statements at the end of the output are 5050, you can see the for loop code is the least amount, and the writing format is more standardized, you can avoid missing variables initialization and so on. Of course, you can nest conditional statements in loops, or you can nest loop statements.
3. Nested use of loops
Nested loop statements in a loop can solve the problem of more judgment and multiple use of loops, but be aware of using the break and continue statements when using nested statements:
(1). The break statement jumps out of the loop body from the inside of the loop and ends the loop prematurely;
(2). The Continue statement skips the statement that has not been executed in the loop body and proceeds to the next loop;
Let's take a look at two examples:
Example 1. Number of 1~100 that cannot be divisible by 7 (nested conditional statements within a loop)
#include <stdio.h>
int main (int argc, const char * argv[]) {
int num;
int sum = 0;
Use a For loop to accumulate numbers that cannot be divisible by 7
for (num=1;num<=100; num++)
{
if (num%7==0)
If a number that is divisible by 7 is encountered during the cycle, jump out of this cycle, using continue
Continue
Sum=sum+num;
}
printf ("1-100 cannot be divisible by 7 for the sum of%d\n", sum);
}
Example 2. If you lose 10 numbers, sort the 10 numbers (that is, bubble sort), this example is the actual application of loop nesting loops.
#include <stdio.h>
int main ()
{
int num[10]={};
int i;
int len=sizeof (num)/sizeof (int);
for (i=0; i<10; i++)
{
printf ("Please enter%d data for the array \ n", i+1);
Input array elements
scanf ("%d", &num[i]);
}
Print array elements
for (i=0;i<len;i++)
{
printf ("%d", num[i]);
}
printf ("\ n");
int C;
The outer loop compares the maximum number of len-d each time
for (int d=0;d<len-1;d++)
{
/* Definition p=1 for num[1] compared to num[0, the first round is compared len-1 times,
The second round compares len-2 times, the third round compares len-3 times, so the loop limit for this here can be written as: len-d
*/
for (int p=1;p<len-d;p++)
If the latter number is less than the previous number, swap the position and move the large number back to the rear
if (Num[p]<num[p-1])
{
C=NUM[P-1];
NUM[P-1]=NUM[P];
Num[p]=c;
}
}
Prints the sorted array position
for (i=0;i<10;i++)
{
printf ("%d", num[i]);
{
return 0;
}
Loops in the actual programming of the use of more scenes, need to be mastered by several exercises.
C Language-Beginner cycle