C的控制语句:循环
1、三种循环方式--->
for
、while
、do while
2、函数
fabs()
3、编写有返回值的函数
三种循环方式
while
示例代码:
# include<stdio.h>
int main(void)
{
long num; long sum = 0L; int status;
printf("请输入一个整型整数进行求和");
printf("(q 或者 quit 退出):");
status = scanf("%ld", &num); // scanf()函数特性,如果scanf()函数接收到一个整数,会把该整数存入num当中,并且返回一个1赋值给status
while (status == 1)
{
sum = sum + num;
printf("请输入一个整型整数进行求和 (q 或者 quit 退出):");
status = scanf("%ld", &num); // 如果用户输入的不是数字,scanf()函数读取失败,返回0,程序退出--->利用的scanf()的双重特性避免了循环过程中的程序交互输入问题
}
printf("这些整型整数的和是:%ld\n", sum);
getchar();
return 0;
}
变式:
# include<stdio.h>
int main(void)
{
long num; long sum = 0L; int status;
printf("请输入一个整型整数进行求和");
printf("(q 或者 quit 退出):");
while ((scanf("%ld", &num)) == 1)
{
sum = sum + num;
printf("请输入一个整型整数进行求和 (q 或者 quit 退出):");
}
getchar();
return 0;
}
注意:
每次循环都被称为一次迭代
循环终止条件:
# include<stdio.h>
int main(void)
{
int n = 0;
while (n < 7) // n < 7就是循环终止条件
{
printf("n = %d\n", n);
n++;
printf("Now n = %d\n", n);
}
printf("结束循环!");
getchar();
return 0;
}
注意:
# include<stdio.h>
int main(void)
{
int n = 0;
while (n < 3)
printf("Now n = %d", n);
n++; // 只有跟在循环条件后面的一条语句是循环语句,第二句开始就不是,所以n不会递增,while的循环条件一直为true
printf("Down!");
return 0;
}
_Bool类型
注意:
bool 不是 c语言的关键字, 但是c99标准和c++把bool当作关键字
# include<stdio.h>
# include<stdbool.h> // 定义了Bool类型
int main(void)
{
long num; long sum = 0L; _Bool input_is_good;
printf("输入一个数");
printf("q 或者 quit 退出:");
input_is_good = (scanf("%ld", &num) == 1);
while (input_is_good)
{
sum = sum + num;
printf("输入一个数");
printf("q 或者 quit 退出:");
input_is_good = (scanf("%ld", &num) == 1);
}
printf("Down!");
getchar();
return 0;
}
For
/**
* @Author: Lucifer
* @Date: 4/16/2023, 4:40:59 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/16/2023, 4:40:59 PM
* Description: A case about for
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
const int NUMBER = 22;
int count;
for (count = 1; count <= NUMBER; count++)
{
printf("Hello World!");
}
getchar();
return 0;
}
字符代替数字作为测试条件和初始值:
# include<stdio.h>
int main(void)
{
char ch;
for (ch = 'a'; ch < 'z'; ch++) {
printf("字符!!!");
}
getchar();
return 0;
}
省略循环表达式写法:
/**
* @Author: Lucifer
* @Date: 4/16/2023, 5:26:45 PM
* @LastEditors: Lucifer
* @LastEditTime: 4/16/2023, 5:26:45 PM
* Description: for循环当中省略表达式的写法
* Copyright: Copyright (©)}) 2023 Your Name. All rights reserved.
*/
# include<stdio.h>
int main(void)
{
int n, ans, time;
ans = 2;
time = 1;
for (n = 3; ans <= 25;)
{
ans = ans * n;
printf("循环次数: %d \n", time);
time++;
}
printf("n = %d, ans = %d\n", n, ans * n);
getchar();
return 0;
}
逗号运算符运用在for
循环中表达多种用法:
/** * @Author: Lucifer * @Date: 4/16/2023, 5:37:23 PM * @LastEditors: Lucifer * @LastEditTime: 4/16/2023, 5:37:23 PM * Description: ,运算符,在一个for循环中,每个条件表达式可以用,来表达多种条件 * Copyright: Copyright (©)}) 2023 Your Name. All rights reserved. */ # include<stdio.h>int main(void)
{
const int FIRST_OZ = 46;
const int 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); } getchar(); return 0;
}
示例代码2:
/** * @Author: Lucifer * @Date: 4/16/2023, 7:41:35 PM * @LastEditors: Lucifer * @LastEditTime: 4/16/2023, 7:41:35 PM * Description: ZenO悖论 * Copyright: Copyright (©)}) 2023 Your Name. All rights reserved. */ # include<stdio.h>int main(void)
{
int t_ct; // 项计数
double time, power_of_2;
int limit;printf("输入你想射几米:"); scanf("%d", &limit); // 箭永远只会行进一半的路程 for (time = 0, power_of_2 = 1, t_ct = 1; t_ct <= limit; t_ct++, power_of_2 *= 2.0) { time += 1.0 / power_of_2; printf("time = %f, 当terms = %d \n", time, t_ct); } getchar(); getchar(); return 0;
}
do...while
示例代码:
/** * @Author: Lucifer * @Date: 4/16/2023, 7:47:13 PM * @LastEditors: Lucifer * @LastEditTime: 4/16/2023, 7:47:13 PM * Description: 猜数字游戏 * Copyright: Copyright (©)}) 2023 Your Name. All rights reserved. */ # include<stdio.h>标签:语句,控制,num,int,printf,循环,2023,include,getchar From: https://www.cnblogs.com/JunkingBoy/p/17324275.htmlint main(void)
{
const int secret_code = 13;
int code_entered;do { printf("有一个内置的数,在10到20之间,猜猜是几?:\n"); printf("请输入你猜的数:\n"); scanf("%d", &code_entered); getchar(); } while (code_entered != secret_code); printf("猜对了,游戏结束!"); getchar(); return 0;
}