C language basics: for statement

1. For loop statement

         In this section we'll learn another way of looping, which is called the for statement. The syntax of the for statement is as follows:

for (initial statement; conditional expression; execute statement after each loop)
{
    printf("%d\n", day);
}

         There are three parts in the parentheses after the for statement, and each part is separated by a semicolon;.

The first part is the initialization statement, which is executed first when the program executes the for statement, and is executed only once, regardless of whether the conditional expression holds.

The second part is the conditional expression. When the conditional expression is established, that is, when its result is true, the program loops through the loop body in the curly brackets.

The third part is the execution statement after each loop, and this part of the statement will be executed every time the loop is executed.

        There is no essential difference between the for statement and the while loop, but it is more concise in form. For example, in the previous section, we wrote a program for the while loop to display 1 to 31:

 

int day = 1;
while (day <= 31)
{
	printf("%d\n", day);
	day++;
}

        And below we will implement a loop program using the for statement, the function is the same as the above program:

 

int day;
for (day = 1; day <= 31; day++)
{
    printf("%d\n", day);
}

        In the for loop statement, first set the value of day to 1, and then judge according to the result of the conditional expression. When the value of the conditional expression is true, execute the contents of the loop body, that is, call the printf function to display the value of day. Every time a loop is executed, the last part of the content in the parentheses, day++, is executed.

        So after the above program is executed 31 times, the value of day becomes 32, the result of the conditional expression day <= 31 is false, and the loop ends.


Second, loop nesting

        For the three types of loop statements that have been learned, loop nesting can be performed, that is to say, a sub-while loop statement is embedded in the loop body of a while statement, a do while sub-loop is embedded in the do while loop body, and the same for loop You can also embed for subloops in . In fact, loop nesting can be used arbitrarily. For example, it is possible to embed do while in while, and embed a for loop in this do while. In order to give readers a better understanding, let's first write a for loop and use an if statement to display the date of January, and wrap the line every 7 days:

int day;
for (day = 1; day < 31; day++)
{
	printf("%3d", day);
	if (day % 7 == 0)
	{
		printf("\n");
	}
}

  1  2  3  4  5  6  7
  8  9 10 11 12 13 14
 15 16 17 18 19 20 21
 22 23 24 25 26 27 28
 29 30

        Of course, this program that displays the date does not consider too many details, such as the relationship between leap years, months and weeks, but it can already be a good illustration of the method of nesting if statements in for loops. Let's use the double loop of the for statement again to implement a program that displays a 9x9 multiplication table:

int i, j;
for (i = 1; i <= 9; i++)
{
	for (j = 1; j <= i; j++)
	{
		printf("%dx%d=%-3d", i, j, i * j);
	}
	printf("\n");
}

1x1=1  
2x1=2  2x2=4  
3x1=3 3x2=6 3x3=9  
4x1=4  4x2=8  4x3=12 4x4=16
5x1=5  5x2=10 5x3=15 5x4=20 5x5=25
6x1=6  6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8  8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81


Welcome to the public account: programming aliens

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325727467&siteId=291194637