#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;
}

c语言之控制语句:循环的更多相关文章

  1. C语言-for循环

    for循环是C语言中的循环语句之一,它的一般形式为for(初值,条件表达式,步长){语句};初值通常是一个赋值语句, 它用来给循环控制变量赋初值: 条件表达式是一个关系表达式, 它决定什么时候退出循环 ...

  2. 怎么绘制C语言选择和循环语句的思维导图

    C语言是一门非常基础的计算机语言,是大部分本科学生的公共专业,在C语言的学习中,选择和循环语句是至关重要的部分,利用思维导图可以有效节约时间并加深知识点记忆. 接下来就为大家介绍一下我用iMindMa ...

  3. C语言利用for循环打印菱形

    C语言利用for循环打印菱形(高度为奇数) 这次用的方法是上下部分分开打印,先打印上部分,再打印下部分. 先举个简单的例子打印,再改进代码,登堂入室从而理解. 例:打印一个高度(高度必须为奇数)为 5 ...

  4. [08 Go语言基础-for循环]

    [08 Go语言基础-for循环] 循环 循环语句是用来重复执行某一段代码. for 是 Go 语言唯一的循环语句.Go 语言中并没有其他语言比如 C 语言中的 while 和 do while 循环 ...

  5. JS 引入方式 基本数据类型 运算符 控制语句 循环 异常

    一.JS引入方式 什么是JavaScript? JavaScript是运行在浏览器端的脚步语言,JavaScript主要解决的是前端与用户交互的问题,包括使用交互与数据交互,JavaScript是浏览 ...

  6. JS脚本语言里的循环

    js脚本语言:  循环:(循环操作某一个功能(执行某段代码)) 四要素: 循环初始值  循环条件  状态改变  循环体 for(穷举  迭代) while 举例:(穷举) 与7相关的数 <scr ...

  7. go语言中间的循环

    在Go语言中只有很少的几个控制结构,它没有while或者do-while循环. 但是它有for.switch.if.而且switch接受像for那样可选的初始化语句.下面来认识一下他们 一.if语句 ...

  8. 电脑小白学习软件开发-C#语言基础之循环重点讲解,习题

    写代码也要读书,爱全栈,更爱生活.每日更新原创IT编程技术及日常实用视频. 我们的目标是:玩得转服务器Web开发,搞得懂移动端,电脑客户端更是不在话下. 本教程是基础教程,适合任何有志于学习软件开发的 ...

  9. [转] Lisp语言:Do循环的使用

    转自http://blog.csdn.net/keyboardota/article/details/8240250 有关Lisp语言中的Do循环,就像很多人说的一样,初看起来太奇怪了,不知道怎么理解 ...

随机推荐

  1. .NET Core[MVC] 利用特性捕捉异常

    声明:本方式适用于MVC.本代码只适用于.NET Core MVC. 先创建一个类继承ExceptionFilterAttribute这个抽象类,并override它的方法OnException. 代 ...

  2. LeetCode 178. 分数排名

    1.题目描述 编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注意,平分后的下一个名次应该是下一个连续的整数值.换句话说,名次之间不应该有“间隔”. +--- ...

  3. Css实现手机端页面强制横屏的方法示例

    样式 ? 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 @media screen ...

  4. 外媒评李开复的《AI·未来》:四大浪潮正在席卷全球

    外媒评李开复的<AI·未来>:四大浪潮正在席卷全球 https://mp.weixin.qq.com/s/oElub0QOYjOROhqN3ULUkg [网易智能讯 9月17日消息]李开复 ...

  5. JS table内容转成二维数组,支持colspan和rowspan

    思路:1.先初始化colspan的数据到数组2.根据rowspan和colspan计算th和td的矩阵二次填充数组 说明:需要引用到第三方库jQuery,table中的th和td行和列跨度必须正确 & ...

  6. Apex 中的自定义迭代器

    迭代器 迭代器(iterator)可以遍历一个集合变量中的每个元素.Apex提供了Iterator接口来让开发者实现自定义的迭代器. Iterator接口 Iterator接口定义了两个函数: has ...

  7. USGS-EROS项目espa-surface-reflectance中的Landsat8 大气校正LaSRC Version 1.3.0模块利用vs2010编译出windows64位版本(四)

    ,支持一些关键问题: 1    数据初始化问题.该问题是指在linux环境下编程标准c并编译,用户定义的变量默认初始值是0,但在windows 64 win7环境中,变量默认初始值是负值极小.... ...

  8. ionic3 导航的应用(页面跳转与参数传递)

    about.html(跳转页面) <ion-content padding> <ion-list> <ion-item *ngFor="let he of co ...

  9. 测者的测试技术手册:自动化单元工具EvoSuie的代码覆盖报告

    EvoSuite是由Sheffield等大学联合开发的一种开源工具,用于自动生成测试用例集,生成的测试用例均符合Junit的标准,可直接在Junit中运行.得到了Google和Yourkit的支持. ...

  10. 云服务器挂载/dev/vdb1磁盘

    1.首先检查是否有一块硬盘还未被挂载 2.如图所示,vdb还未被使用,开始分配它吧 [root@localhost ~]# fdisk /dev/vdb Command (m for help): n ...