C控制语句:循环
再探while循环
#include <stdio.h>
int main(){
long num;
long sum = 0L;
int status;
printf("Please enter an integer to be summed \n");
printf("q to quit:");
status = scanf("%ld",&num); //scanf函数返回读取值个数
while (status == 1)
{
sum = sum + num;
printf("Please enter next integer (q to qiut): \n");
status = scanf("%ld",&num);
}
printf("those integers sum to %ld.\n",sum);
return 0;
}
while语句
while循环结构
终止while循环
为避免程序无限循环,必须让测试表达式的值有变化,达到循环终止的目的。
何时终止循环
#include <stdio.h>
int main(){
int n = 5;
while (n < 7)
{
printf("n = %d",n);
n++;
printf("Now n =%d\n",n);
}
printf("The loop has finished.\n");
return 0;
}
n = 5Now n =6
n = 6Now n =7
The loop has finished.
while:入口条件循环
index = 10;
while (index < 5) //index =3; 可以运行
printf("this is a single loop");
语法要点
#include <stdio.h>
int main(){
int n = 0;
while (n ++ <3); //这里的分号将while语句限制住,直到循环结束才会执行下一条语句
printf("n is %d\n",n);
printf("That's all this program does.\n");
return 0;
}
n is 4
That's all this program does.
// 不加分号
n is 1
n is 2
n is 3
That's all this program does.
用关系运算符和表达式比较大小
关系运算符
运算符 | 含义 |
---|---|
< | 小于 |
<= | 小于等于 |
== | 等于 |
>= | 大于等于 |
> | 大于 |
!= | 不等于 |
真值假值
#include <stdio.h>
int main(){
int ture_val,false_val;
ture_val = (10>2);
false_val = (10 == 2);
printf("true = %d; false = %d \n",ture_val,false_val);
return 0;
}
true = 1; false = 0
其他真值
#include <stdio.h>
int main(){
int n =3;
while (n)
printf("%2d is true\n",n--);
printf("%2d is false\n",n);
n = -3;
while (n)
printf("%2d is true\n",n++);
printf("%2d is false\n",n);
return 0;
}
3 is true
2 is true
1 is true
0 is false
-3 is true
-2 is true
-1 is true
0 is false
新的_Bool类型
_Bool是C语言中布尔变量的类 型名。_Bool类型的变量只能储存1(真)或0(假)。如果把其他非零数值 赋给_Bool类型的变量,该变量会被设置为1。
_Bool input_is_good;
优先级和关系运算符
关系运算符优先级比算术运算符低,比赋值运算符高。
运算符 | 结合律 |
---|---|
() | 自左往右 |
- + ++ – sizeof | 自右往左 |
* / | 自左往右 |
+ - | 自左往右 |
< > <= >= | 自左往右 |
== != | 自左往右 |
= | 自右往左 |
不确定循环和技术循环
1.必须初始化计数器
2.计数器与有限的值作比较
3.每次循环时递增计数器
for循环
for形式:
for (initialize; test; update )
statement
initialize表达式在执行for语句之前只执行一次;然后对test表达式求值,如果表达式为真 (或非零),执行循环一次;接着对update表达式求值,并再次检查test表达 式。
for循环结构
for的灵活性
使用递减符
for (secs = 5; secs > 0; secs--)
递减/增间隔
for (n = 2; n < 60; n = n + 13)
for (debt = 100.0; debt < 150.0; debt = debt * 1.1)
字符代替数字
for (ch = 'a'; ch <= 'z'; ch++)
表达式任意合法
for (x = 1; y <= 75; y = (++x * 5) + 50) //第三个表达式可以不写 只要能终止
for (printf("Keep entering numbers!\n"); num != 6;)
其他赋值运算符
a +=20 | a=a+20 |
---|---|
b -= 20 | b = b-20 |
c*=20 | c = c*20 |
d/=2.7 | d = d/2.7 |
e%=3 | e = e%3 |
逗号运算符
逗号运算符把两个表达式连接成一个表达式,并保证最左边的表达式最先求值。
// 一类邮资
#include <stdio.h>
int main(){
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);
}
return 0;
}
出口循环条件:do while
#include <stdio.h>
int main(){
const int secret_code = 13;
int code_entered;
do
{
printf("To enter the triskaiphoboa therapy club,\n");
printf("please enter the secret code number:");
scanf("%d",&code_entered);
} while (code_entered != secret_code);
printf("Congratulations! you are cured!\n");
return 0;
}
do while循环结构
嵌套循环
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main(){
int row;
char ch;
for (row = 0; row < ROWS; row++) //outer lopp
{
for (ch = 'A'; ch < ('A'+CHARS); ch++) //inner loop
{
printf("%c",ch);
}
printf("\n");
}
return 0;
}
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
ABCDEFGHIJ
变式
#include <stdio.h>
#define ROWS 6
#define CHARS 6
int main(){
int row;
char ch;
for (row = 0; row < ROWS; row++)
{
for (ch = ('A'+row); ch < ('A'+CHARS); ch++)
{
printf("%c",ch);
}
printf("\n");
}
return 0;
}
ABCDEF
BCDEF
CDEF
DEF
EF
F
数组简介
数组是按顺序储存的一系列类型相同的值。整个数组有一个数组名,通过整数下标访问数组中单独的项或元素(element)。
//声明
float debts[20]; //20 ——> 数组元素个数
debts[0]=12.3; //数组中第一个element
debts[19] = 57.8 //数组中最后一个element
debts[20] = 65.88 //数组元素不存在
字符数组
字符串
用于识别数组元素的数字被称为下标(subscript)、索引(indice)或偏移量(offset)。下标必须是整数,而且要从0开始计数。
for 循环中使用数组
#include <stdio.h>
#define SIZE 10
#define PAR 72
int main(){
int index,score[SIZE];
int sum = 0;
float avreage;
printf("Enter %d golf scores:\n",SIZE);
for (index = 0; index < SIZE; index++)
{
scanf("%d",&score[index]); /* 注意使用& */
}
printf("The scores read in are as follw:\n");
for (index = 0; index < SIZE; index++)
{
printf("%5d",score[index]);
}
printf("\n");
for (index = 0; index < SIZE; index++)
{
sum += score[index];
}
avreage = (float)sum / SIZE;
printf("Sum of scores = %d,average = %.2f\n",sum,avreage);
printf("That's a handicap of %.0f.\n",avreage - PAR);
return 0;
}
使用函数返回值的循环示例
#include <stdio.h>
double power(double n,int p); //返回double类型值 第一个参数double类型第二个int类型
int main(){
double x,xpow;
int exp;
printf("Enter a number and the positive integer power");
printf("to which \n the number will be raised. Enter q");
printf(" to quit.\n");
while (scanf("%lf%d",&x,&exp) == 2)
{
xpow = power(x,exp);
printf("%.3gto 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");
return 0;
}
double power(double n,int p){
double pow =1;
int i;
for ( i = 1; i <= p; i++)
{
pow *=n;
}
return pow;
}
复习题
使用嵌套循环,编写一个程序打印以下图案:
#include <stdio.h>
int main(){
int i,j;
for (i=0;i<4;i++)
{
for (j=0;j<8;j++)
{
printf("%$");
}
printf("\n");
}
return 0;
}
#include <stdio.h>
int main(){
int k;
for (k = 1, printf("%d: HI\n",k);printf("k = %d\n",k),k*k<26;k+=2,printf("now k is %d\n",k))
printf("k is %d in the loop\n",k);
return 0;
}
1: HI
k = 1
k is 1 in the loop
now k is 3
k = 3
k is 3 in the loop
now k is 5
k = 5
k is 5 in the loop
now k is 7
k = 7