c语言之控制语句:循环

时间:2021-05-22 12:41:47
#include<stdio.h>

int main(void)
{
long num;
long sum = 0L;
int status;
printf("Please enter an integer to be summed:");
printf("(q to quit):");
status = scanf_s("%ld", &num);
while (status)
{
sum = sum + num;
printf("Please enter next integer (q to quit):");
status = scanf_s("%ld", &num);
}
printf("Those integers sum to %ld.\n", sum);
system("pause");
return 0;
}
status = scanf_s("%ld",&num);
while(status=1)
{
/*循环行为*/
status = scanf_s("%ld",&num)
}
//替换代码
while(scanf_s("%ld",&num)=1)
{
/*循环行为*/
status = scanf_s("%ld",&num)
}

while语句

while(expression)
statement

statement部分可以是以分号结尾的简单语句,也可以是用花括号括起来的复合语句。其中,如果expression为真,执行。

在构建while循环时,必须让测试表达式的值有所变化。

while(expression)
printf("sth");//如果没有花括号,只有紧跟着while后面的第一条语句才算循环
other statement
/*不加分号*/
#include<stdio.h> int main(void)
{
int n = 0;
while (n++ < 3)
printf("n is %d\n", n);
printf("That's all this program does.\n");
system("pause");
return 0;
}
/*
result:
n is 1
n is 2
n is 3
That's all this program does.
*/ /*加分号*/
#include<stdio.h> int main(void)
{
int n = 0;
while (n++ < 3); //加分号则会让编译器认为这句号是单独的一句
printf("n is %d\n", n);
printf("That's all this program does.\n");
system("pause");
return 0;
}
/*
result:
n is 4
That's all this program does.
*/

用关系运算符和表达式比较大小

运算符 含义
< 小于
<= 小于或等于
== 等于
>= 大于或等于
> 大于
!= 不等于

注:当使用浮点数时,尽量只是用“<”或“>”,因为浮点数的舍入误差会导致在逻辑上应该相等的两数却不相等。

value_1 == value_! //判断
value_1 = value_2 //赋值
#include<math.h>
#include<stdio.h> int main(void)
{
const double ANSWER = 3.14159;
double response;
printf("What is the value of pi?\n");
scanf_s("%f", &response);
while (fabs(response - ANSWER) > 0.0001) //fabs()函数取绝对值
{
printf("Try again!\n");
scanf_s("%lf", &response);
}
printf("Close enough!\n");
system("pause");
return 0;
}
新的_Bool类型
#include<stdio.h>

int main(void)
{
long num;
long sum = 0L;
_Bool input_is_good; //定义Bool类型
printf("Please enter an integer to be summed");
printf("(q to quit):");
input_is_good = (scanf_s("%ld", &num) == 1);
while (input_is_good)
{
sum = sum + num;
printf("Please enter next integer(q to quit):");
input_is_good = (scanf_s("%ld", &num) == 1);
}
printf("Those integers sum to %ld.\n", sum);
return 0;
}
优先级和关系运算符

高优先级组:<、<=、>、>=

低优先级组:==、!=

不确定循环和计数循环

#include<stdio.h>

int main(void)
{
const int NUMBER = 22;
int count = 1;
while (count <= NUMBER) {
printf("Be my Valentine!\n");
count++;
}
system("pause");
return 0;
}

for 循环

#include<stdio.h>

int main(void)
{
const int NUMBER = 22;
int count = 1;
for (count = 1; count <= NUMBER; count++)//第一个是表达式初始化,第二个是测试条件,第三个是表达式执行更新。
printf("Be my Valentine!\n");
system("pause");
return 0;
}
#include<stdio.h>

int main(void)
{
char ch;
for (ch = 'a'; ch <= 'z'; ch++)
printf("The ASCII value for %c is %d.\n", ch, ch);
system("pause");
return 0;
}

可以省略一个或多个表达式(但是不能省略分号),只要在循环体中包含能结束循环的语句即可。另外,第一个表达式不一定要给变量赋初值,也可以使用printf()。但是,在执行循环体的其他部分之前,只对第一个表达式求值一次或执行一次。

#include<stdio.h>

int main(void)
{
int num = 0;
for (printf("Keep entering number!\n"); num != 6;)
scanf_s("%d", &num);
printf("That's one I want!\n");
return 0;
}

其他赋值运算符

表达式 含义
scores += 20 scores = scores + 20
dimes -= 2 dimes = dimes - 20
bunnies *= 2 bunnies = bunnies * 20
time /= 2.73 time = time / 2.73
reduce %= 3 reduce = reduce % 3

逗号运算符

#include<stdio.h>

int main(void)
{
const int FIRST_OZ = 46, NEXT_OZ = 20;
int ounces, cost;
printf("ounces cost\n");
for (ounces = 1, cost = FIRST_OZ; ounces <= 16; ounces++, cost += NEXT_OZ) //同时有多个值,且从左到右顺序执行
printf("%5d $%4.2f\n", ounces, cost / 100.0);
system("pause");
return 0;
}
houseprice = 249,500 //分为houseprice=249和500这两条表达式语句
houseprice =(249,500)//等于houseprice=500
char ch,date;//分隔符

出口条件循环:do while

#include<stdio.h>

int main(void)
{
const int secret_code = 13;
int code_entered;
do
{
printf("To enter the yuan therapy clud,\n");
printf("please enter the scret code number:");
scanf_s("%d", &code_entered);
} while (code_entered != secret_code);
printf("Congratulation! You are cured!\n");
system("pause");
return 0;
} /*
do
statement
while (expression);
*/

do while循环在执行完循环体后才执行测试条件,所以至少执行循环体一次,while循环体适用于那些至少要迭代一次的循环。

嵌套循环

/*打印字母串*/
#include<stdio.h>
#define ROWS 6
#define CHARS 10 int main(void)
{
int row;
char ch;
for (row = 0; row < ROWS; row++) {
for (ch = 'A'; ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n");
}
system("pause");
return 0;
}

数组

float debts[n];//声明一个含有20个元素的数组
debts[n]=num;//为数组元素赋值,编译器不会查找元素不在等的错误

用于识别数组元素的数字被称为下标、索引或偏移量。

/*打印字母串*/
#include<stdio.h>
#define SIZE 10
#define PAR 72 int main(void)
{
int index, score[SIZE];
int sum = 0;
float average;
printf("Enter %d golf scores:\n", SIZE);
for (index = 0; index < SIZE; index++)
scanf_s("%d",&score[index]);
printf("\n");
for (index = 0; index < SIZE; index++)
sum += score[index];
average = (float)sum / SIZE;
printf("Sum of scores = %d,average = %.2f\n", sum, average);
printf("That's a handicap of %.0f.\n", average - PAR);
system("pause");
return 0;
}

使用函数返回值的循环

#include<stdio.h>
double power(double n, int p);
/*C编译器运行到这里还不知道power函数是什么,所以要先声明该函数的返回值类型等,而如果该函数写在main函数的前面,则不需要在此处声明该函数*/ int main(void)
{
double x, xpow;
int exp;
printf("Enter a number and the positive integer power");
printf(" to which\nthe number will be raised. Enter q");
printf(" to quit.\n");
while (scanf_s("%lf%d", &x, &exp) == 2)
{
xpow = power(x, exp);
printf("%.3g to the power %d is %.5g.\n", x, exp, xpow);
printf("Enter next pair of numbers or q to quit.\n");
}
printf("Hope you enjoyed this power trip -- bye!\n");
system("pause");
return 0;
} double power(double n, int p)
{
double pow = 1;
int i;
for (i = 1; i <= p; i++)
pow *= n;
return pow;
}