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

        对于已经学过的三种循环语句来说,都可以进行循环嵌套,也就是说一个while语句的循环体中嵌入一个子while循环语句,do while循环体中嵌入do while子循环,同样for循环中也可以嵌入for子循环。其实,循环嵌套可以任意使用,例如在while中嵌入do while,并在这个do while中再嵌入一个for循环,都是可以的。为了让读者更好的理解我们先来编写一个for循环配合使用if语句显示一月的日期,每显示7天就进行一次换行:

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

        当然,这个显示日期的程序并没有考虑太多细节问题,例如没有考虑闰年、月份和星期之间的关系,不过已经可以很好的说明在for循环中嵌套if语句的方法了。我们再来用for语句的双循环来实现一个显示9x9乘法表的程序:

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 


欢迎关注公众号:编程外星人

Guess you like

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