循环语句有:while、do while 、for。
三个循环语句都有一个判断表达式,通过此表达式判断是否执行循环,通常满足表达式则为真,不满足表达式则为假,
,所以在了解循环之前需要简单了解一下表达式里面常用的运算符,包括关系运算符,赋值运算符,逗号运算符。
运算符 | 含义 |
---|---|
< | 小于 |
<= | 小于等于 |
== | 等于 |
> | 大于 |
>= | 大于等于 |
!= | 不等于 |
a += 2 —>a = a + 2
a -= 2 —>a = a - 2
a %= 2 —>a = a % 2
a /= 2 —>a = a /2
a *= 2 —>a= a * 2
对于C语言来说,表达式一定有一个值,表达式为真的值是1,表达式为假的值是0
这里将在介绍while循环处说明。
会在for循环讲到
注意:三个循环都有statement部分,执行的一个单元,当执行分几步时,可以利用括号。
形式:
while(expression)
statement;属于入口条件循环:先判断再执行
在expression部分为假或为0 之前,重复执行statement部分
#include
int main()
{
int num = 0;
int sum = 0;
int status = 0;
printf("please enter an integer to be summed\n");//提示输入一个加数
printf("(q to quit)\n");//输入退出
status = scanf("%d", &num);//输入
while (status == 1)//判断
{
//while语句只会读取下面一个语句或者代码块
sum = num + sum;
printf("%d", sum);
printf("please enter next integer to be summed\n");
status = scanf("%d", &num);
}//可以执行多个数相加
printf("those intergers sum to %d", sum);//打印结果
return 0;
}
要是想while终止,可以用break和if语句或者改变测试表达式的值
#include
int main()
{
int index = 1;
while (index < 5)
printf("good morning \n");//只有这两行的话while循环不会停止
return 0;
}
#include
int main()
{
int index = 1;
while (index < 5)
{
printf("good morning\n");
if (index < 3)
{
printf("hello\n");
break;
}
else
{
printf("no\n");
break;
}
}
return 0;
}//结果:输出goodmorning以及hello后循环停止
#include
int main()
{
int index = 1;
while (index < 5)
{
printf("good morning \n");
index++;
}//注意:这里需要括号将两行代码形成一个代码块,因为while只会识别下面一个语句
return 0;
}
#include
int main()
{
int n = 5;
while (n < 7)
{
printf("n = %d\n", n);
n++;
printf("now n = %d\n", n);
}
printf("the loop has finished .\n");
return 0;
}//打印到 now n = 7结束,当n 等于7时,不满足n<7的条件,不再循环
#include
int mian()
{
int index = 10;
while (index++ <= 5)
printf("haha\n");
return 0;/*这个循环不会开始,因为while是入口循环
一开始index就不满足 <= 5, 所以直接跳过循环。*/
}
比如可以这样写:比较字符:
#include
int main()
{
char ch = 'w';
int count = 1;
printf("please input char\n");
while (ch != '$')
{
count++;
scanf("%c", &ch);
printf("%d\n", count);
printf("$-->quit\n");
}
return 0;
}
尽量使用<>, 不要使用 = ,因为浮点数在系统中四舍五入可能导致逻辑上相等的两个数不相等
浮点数比较:
#include
#include
int main()
{
const double ANSWER = 3.14159;
double reponse = 0;
printf("what is the value of pi?\n");
scanf("%lf", &reponse);
while (fabs(reponse - ANSWER) > 0.0001)
{
printf("try again!\n");
scanf("%lf", &reponse);
}
printf("close enough!\n");
return 0;
}
循环会一直提示用户输入,除非与正确值之间相差值小于0.0001
#include
int main()
{
int mmi1 = (10 > 2);
int mmi2 = (10 < 2);
printf("true = %d\n", mmi1);//true = 1
printf("false = %d\n", mmi2);//false = 0
return 0;
}
#include
int main()
{
while(1)
printf("haha\n");
return 0;
}
#include
int main()
{
int n = 3;//这里用整数,字符串,字符都行
while (n)
{
printf("%d is true\n", n);
n--;
}//当n=0时会停止
printf("%d is false\n");//
while ('w')
{
printf("'w' is true\n");//w is true
break;
}
return 0;
}
关键字: _Bool
布尔类型只能储存1或者0,如果是非零其他值,会被转换成1
#include
int main()
{
_Bool index = "nihao ";//"3"可以,'w'也可以,"nihao"也可以
while (index)
{
printf("hehe\n");
break;
}
return 0;
}
形式:
do
statement
while(expression);属于出口条件循环先执行一次,再判断是否执行下一次
#include
int main()
{
int sum = 0;
int count = 0;
int test = 0;
printf("please input an integer to be summed\n");
test = scanf("%d", &count);
do
{
printf("please input next integer\n");
sum = count + sum;
test = scanf("%d", &count);
}
while (test == 1);
printf(" sum = %d\n", sum);
return 0;
}
形式:
for(initialize;test;update)
statement;属于入口条件循环
在test为假或者为0之前,重复执行statement部分
注意:入口条件循环
#include
int main()
{
const int NUMBER = 5;
int a = 0;
for (a = 1; a <= NUMBER; a++)//NUMBER限制了a加的次数
printf("haha\n");
return 0;
}
#include
int main()
{
const int NUMBER = 6;
int count = 0;
int count2 = 0;
for (count = 1; count <= NUMBER; count++)
{
count2 = count * count * count;
printf("%d %d\n", count, count*count*count);
}//上面的count*count*count可以用count2代替
return 0;
}
#include
int main()
{
const int NUMBER = 1;
int count = 0;
for (count = 7; count >= NUMBER; count--)
{
printf("%d seconds!\n", count);
}
printf("begin!");
return 0;
}
:
#include
int main()
{
const int NUMBER = 1;
int count = 7;
while (count >= NUMBER)
{
printf("%d seconds!\n", count);
count--;
}
printf("begin!\n");
return 0;
}
#include ;
int main()
{
const int NUMBER = 1;
int count = 7;
do
{
printf("%d seconds!\n", count);
count--;
} while (count >= NUMBER);
printf("begin!\n");
return 0;
}
结果与前面一样
#include
int main()
{
int a = 0;
for (a = 2; a < 100; a = a + 15)
printf("%d\n", a);
return 0;
}
#include
int main()
{
char ch;
for (ch = 'a'; ch <= 'f'; ch++)
printf("the ascll value for %c is %d.\n", ch, ch);
return 0;
}//字符在内部以整数形式储存的,
比较时,时通过转换成整数来比较的。
比如限制立方的大小
#include
int main()
{
int count = 0;
for (count = 1; count * count * count <= 216; count++)
printf("%d %d\n", count, count*count*count);
return 0;
}/*注意:若在for语句前声明一个变量用与test位置,
会出现错误--顺序没有考虑到到位
//#include
//int main()
//{
// int count = 0;
// int number = 0;
// for (count = 1; number <= 216; count++)
// printf("%d %d\n", count, number);
// return 0;
//}
//*/
**例子1**
#include
int main()
{
int x = 0;
int y = 0;
for (x = 1; y <= 75; y = (++x * 5) + 50)
//第三个表达式里面x值一直在变
printf("%10d %10d\n", x, y);
return 0;
}
例子2
每次乘上一个固定的值
#include
int main()
{
double debt;
for (debt = 100.0; debt < 150; debt = debt * 1.1)
printf("your debt is now $%.2f.\n ",debt);
return 0;
}
#include
int main()
{
int ans = 2;
int n = 0;
for (n = 3; ans <= 25;)//省略了第三个表达式
ans = ans * n;
printf("n = %d ; ans = %d", n, ans);
return 0;
}
/#include<stdio.h>
//int main()
//{
// for (;;)/*省略了三个表达式,注意:
// 省略第二个表达式被视为真*/
// printf("haha\n");
// return 0;
//}
#include
int main()
{
int a = 0;
int b = 1;
for (printf("haha\n"); a < b; a++)
//这里第一个表达式换成了输出函数,一样可以执行
printf("xixi\n");
return 0;
}
#include
#define NUM 10
#define MMI 72
int main()
{
int index = 0;
int sorce[NUM] = {
0 };
int sum = 0;
float ave = 0;
printf("please input your sorce\n");
for (index = 0; index <= 9; index++)
scanf("%d", &sorce[index]);//输入是个数
for (index = 0; index <= 9; index++)
printf("%d ", sorce[index]);//检验那十个数
printf("\n");
for (index = 0; index <= 9; index++)
sum += sorce[index];//求和
ave = sum / NUM;//求平均数
printf("sum is %d", sum);
printf("ave is %.2f", ave);
return 0;
}
```c
这里用一个例子来解释:
#include
int main()
{
const int COUNT = 40;
const int NUMBER = 20;
int cost, ounces;
printf("ounces cost\n");
for (ounces = 1, cost = COUNT; ounces < COUNT; ounces++)
printf("%d",ounces);
return 0;
}
//这里可以同时给ounces和cost都赋值,但是有顺序
//ounces比cost先赋值
//逗号运算符对于顺序有要求:
//后面用不到的话不用要求顺序
#include
int main()
{
char ch = 'a';
int b = 1;
int c = 6;
for (b = 1; b <= c; b++)//这个后面不能加分号,否则会跳过该语句
{
for (ch = 'a'; ch <= 'f'; ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}
#include
int main()
{
char ch = 'a';
int b = 1;
int c = 6;
for (b = 1; b <= c; b++)
{
for (ch = ('a'+b); ch <= 'f'; ch++)
printf("%c", ch);
printf("\n");
}
return 0;
}