1、if 语句;示例程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include<stdio.h>
int
main()
{
const
int
FREEEZING=0;
float
temperature;
int
cold_days=0;
int
all_days=0;
printf
(
"enter the list of daily low temperatures.\n"
);
printf
(
"use Celsius,and enter q to quit.\n"
);
while
(
scanf
(
"%f"
,&temperature)==1)
{
all_days++;
if
(temperature<FREEEZING)
cold_days++;
}
if
(all_days!=0)
printf
(
"%d days total:%.lf%% were below freezing.\n"
,all_days,100.0*(
float
) cold_days/all_days);
if
(all_days==0)
printf
(
"\a No data entered!\n"
);
//报警提示一声并无输入
return
0;
}
|
运行结果:
注释程序:while循环的判断条件利用scanf()的返回值遇到非数字输入时循环终止。用float而不是int来声明temperature,这样程序就能接受如上输入格式,复数也是可以的。
◆if()语句被称为分支语句(branching statement)或者是选择语句(selection statement),因为它提供了一个交汇点,在此处程序需要选择两条分支中的一个继续前进,一般的形式如下:if(expression)
statement
如果expression值为真就执行statement;
◆if 还可以是符合语句:例如:if(a>b)
{
c++;
printf(" ");
}注意即使if使用了一个复合的语句,整个if结构仍将被看做是一个简单的语句。
2、if else 语句
注意if()复合语句的完整性,一对{}表征结束。
1
2
3
4
5
6
|
if
(x>0)
{
printf
(
" "
);
x++;
}
else
printf
(
" "
);
|
3、介绍getchar()和putchar()
getchar()函数没有返回值,它返回来自输入设备的下一个字符,例如:
ch=getchar()就相当于scanf("%c",&ch);
putchar()函数打印他的参数,putchar就相当于printf("%c",ch);
ctype.h系列字符函数
实例程序;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include<stdio.h>
#include<ctype.h>
int
main()
{
char
ch;
while
((ch=
getchar
())!=
'/n'
)
{
if
(
isalpha
(ch))
//如果是一个字母
putchar
(ch+8);
//输出其后的第八个
else
putchar
(ch);
//否则,原样输出,比如逗号啥的 都照常打印。
}
putchar
(ch);
return
0;
}
|
运行结果:
还可以适用于简单的加密算法呢,不这过个安全度太低,还是比较适合于平时简单的练习,
4、判断素数,如果不是求因子。
示例程序:
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
|
#include<stdio.h>
//#include<stdbool.h>
int
main()
{
unsigned
long
num;
unsigned
long
div
;
//约数
int
isPrime;
//素数的标志
printf
(
"please enter the integer for analysis "
);
printf
(
"enter q to quit.\n"
);
while
(
scanf
(
"%lu"
,&num)==1)
{
for
(
div
=2,isPrime=1;
div
*
div
<=num;
div
++)
{
if
(num%
div
==0)
{
if
((
div
*
div
)!=num)
printf
(
"%lu is divisible by %lu and %lu.\n"
,num,
div
,num/
div
);
else
printf
(
"%lu is divisible by %lu.\n"
,num,
div
);
isPrime=0;
//不是素数
}
}
if
(isPrime)
printf
(
"%lu is prime.\n"
,num);
printf
(
"please enter the another number to anlysis:"
);
printf
(
"enter q to quit.\n"
);
}
printf
(
"bye.\n"
);
return
0;
}
|
运行结果:
5、if语句使用总结;
形式1:
1
2
|
if
(expression)
statement
//如果expression为真,则执行statement。
|
形式2:
1
2
3
4
|
if
(expression)
statement 1
else
statement 2
//如果expression为真,则执行statement1,如果为假,则执行statement 2.
|
形式3:
1
2
3
4
5
6
|
if
(expression 1)
statement 1
else
if
(expression 2)
statement 2
else
statement 3
//如果expression 1 为真,则执行statement 1.expression1为假而expression2 为真则执行statement 2,否则这两个表达式都为假,则执行表达式3.
|
6、逻辑运算符
注意范围中应用:&&
可以把&&应用于测试范围,例如要检查90到100范围内的得分,可以这样做:
if(range>=90 && range<=100)
printf("good show!");
而如果你写成了:
if(90<=range<=100)
printf("good show!");//该代码是语义错误,计算机胡给出警告可能,不是编译错误。因为对于<=运算符的求值顺序是从左向右的,所以会把该测试表达式的值解释如下:(90<=range)<=100.自表达式90<=range 的值为0或者是1,但是不管是0或者是1,对于后面的<=100都是对的,所以此时你无论输入什么数进去都不会进行筛选了。
本文转自 lillian_trip 51CTO博客,原文链接:http://blog.51cto.com/xiaoqiaoya/1952763,如需转载请自行联系原作者