C语言第七节 FOR 语句

说明:因为正在探讨微信公众号,新浪博客和本博客联动,所以可能不系统,不连贯。所以,请同时关注我的微信公众号(Mark学编程)目前微信公众号最为系统。今后,尽量在这里最全最系统。
我还是喜欢用英语来描述有关定义。
There are plenty of different ways to write a program for a particular task. For example, while and for and do-while can be used to do the same job. As we know, these are called iterative control statements. And these statements allow us to execute one or more program statements repeatedly. Now Let’s study the for statement.
The general format of the for statement is:

for (initial expression; continue condition; increment expression)
{
statement;
statement;
......
statement;
}

The for statement is a loop, a generalization of the while. if you compare it with previous learned while, its operation looks more clear.
after for there is a parenthese, there are three parts, separated by semicolons. The initial expression is executed before the loop proper is entered. The second part is the test or condition that controls the loop.This continue condition is evaluated. if it is true, the body of the loop is executed. Then the increment expression is executed and the continue condition re-evaluated.The loop terminates if the continue condition has become false. The initial, continue and increment can be any expressions.
中文解释:注意不是严格的英语翻译,只是为了充分表达。
for 循环语句格式是
先写for后面加个括号,括号内有三部分。第一部分是循环变量赋初值,在进入循环前执行,第二部分是循环条件,求这个表达式的值,如果为true,则执行循环语句,就是大括号内的语句。然后执行第三部分。 再然后重新开始第二部分,如果第二部分求值为false,则整个循环结束。另外大括号一般称作循环体。
相信已经说的很清楚了,这个for和while有什么区别,练习的多了,自然清楚了,学习编辑就是不断写代码,测试代码。下面举两个例子,请自行运行测试。后面的是一个嵌套的for.

int main()
{
int i;
for ( i=1; i <= 10; i++)
{
prinft ("%d\n", i);
}
return 0;
}
int main()
{
int num, i;
printf ("please input a number ");
scanf ("%d", &num);
printf ("Number    Square   Cube\n");
printf ("--------------------------------------\n");
for ( i = 1; i <=num; i++)
{
printf ("%4d    %4d   %4d\n", i, i * I, i * i * i);
}
return 0;
}

一定要测试吆。
今天就到这里。明天再见,你可以关注我的微信公众号–Mark学编程,也可以关注我的微博博客也是Mark学编程。当然csdn也是尽量和其他两个同步。

猜你喜欢

转载自blog.csdn.net/Mark21577/article/details/85370099