文章目录
选择语句(if else语句)
在C语言中,使用if
和else
关键字对条件进行判断。请先看下面的代码:
#include <stdio.h>
//选择语句
int main()
{
int age = 0;
printf("你的年龄为:");
scanf("%d", &age);
if (age >= 18)
{
printf("恭喜你,你已经成年了\n");
}
else
{
printf("很抱歉,你还未成年呢\n");
}
return 0;
}
可能的运行结果:
你的年龄为:18
恭喜你,你已经成年了
或者:
你的年龄为:17
很抱歉,你还未成年呢
if 和 else 是两个新的关键字,if 意为“如果”,else 意为“否则”,用来对条件进行判断,并根据判断结果执行不同的语句。
总结起来, if else
的结构为:
if(判断条件){
语句块1
}
else{
语句块2
}
意思是,如果判断条件成立,那么执行语句块1,否则执行语句块2 。其执行过程可表示为下图:
如果语句块中只有一个语句,也可以省略{ }
求两数较大者:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
int max = 0;
scanf("%d %d", &a, &b);
if (a > b)
{
printf("max=%d\n", a);
}
else
{
printf("max=%d\n", b);
}
return 0;
}
运行结果:
10 20
max=20
多个if else
语句
if else 语句也可以多个同时使用,构成多个分支,形式如下:
if(判断条件1){
语句块1
} else if(判断条件2){
语句块2
}else if(判断条件3){
语句块3
}else if(判断条件n){
语句块n
}else{
语句块m
}
意思是,从上到下依次检测判断条件,当某个判断条件成立时,则执行其对应的语句块,然后跳到整个 if else 语句之外继续执行其他代码。如果所有判断条件都不成立,则执行语句块n,然后继续执行后续代码。
也就是说,一旦遇到能够成立的判断条件,则不再执行其他的语句块,所以最终只能有一个语句块被执行。
例:
#include<stdio.h>
int main()
{
int n = 0;
printf("请输入你的成绩:");
scanf("%d",&n);
if (n >= 90 && n <= 100)
{
printf("你的成绩等级为A\n");
}
else if (n >= 80 && n <= 90)
{
printf("你的成绩等级为B\n");
}
else if (n >= 70 && n < 80)
{
printf("你的成绩等级为C\n");
}
else if (n >= 60 && n <= 70)
{
printf("你的成绩等级为D\n");
}
else if (n >= 0 && n <= 60)
{
printf("你的成绩等级为E\n");
}
else
{
printf("你输入的成绩无效\n");
}
return 0;
}
运行结果:
请输入你的成绩:79
你的成绩等级为C
或者:
请输入你的成绩:105
你输入的成绩无效
在使用 if 语句时还应注意以下两点:
- 在 if 语句中,判断条件必须用括号括起来。
- 语句块由
{ }
包围,但要注意的是在}
之后不需要再加分号;
(当然加上也没错)。
if语句的嵌套
if 语句也可以嵌套使用,例如:
#include <stdio.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
if (a != b)
{
if (a > b) {
printf("a>b\n");
}
else {
printf("a<b\n");
}
}
else
{
printf("a == b");
}
return 0;
}
运行结果:
10 20
a<b
20 10
a>b
20 20
a == b
if 语句嵌套时,要注意 if 和 else 的配对问题。
C语言规定,else 总是与它前面最近的 if 配对
循环语句(while语句)
while
循环
while循环的一般形式为:
while(表达式){
语句块
}
例1:
#include <stdio.h>
//打印1-11间的奇数
int main()
{
int n = 0;
while (n<=10)
{
n = n + 1;
printf("%d\n", n);
n++;
}
return 0;
}
运行结果:
1
3
5
7
9
11
例2:
#include <stdio.h>
//计算1-100的和
int main()
{
int n = 0;
int sum = 0;
while (n <= 100)
{
sum += n;
n++;
}
printf("%d\n", sum);
return 0;
}
运行结果:
5050
函数
用函数求2个任意整数和
#include <stdio.h>
int ADD(int x, int y)
{
return x + y;
}
int main()
{
int n1 = 0;
int n2 = 0;
int sum = 0;
printf("请输入两个数\n");
scanf("%d %d", &n1, &n2);
sum = ADD(n1, n2);
printf("两数之和=%d",sum);
return 0;
}
函数的特点就是简化代码,代码复用
数组
数组定义
int arr[10] = {1,2,3,4,5,6,7,8,9,10};//定义一个整形数组,最多放10个元素
数组的下标
C语言规定:数组的每个元素都有一个下标,下标是从0开始的。
数组可以通过下标来访问的。
比如:
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
如果数组10个元素,下标的范围是0-9
int arr[10] | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
下标 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
例:
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
printf("%d\n", arr[9]);
return 0;
}
运算结果:
10
数组的使用
#include <stdio.h>
//打印数组里的每一个数
int main()
{
int n = 0;
int arr[10] = { 1,3,5,7,9,11,13,15,17,19 };
for (n = 0; n < 10; n++)
{
printf("%d\n", arr[n]);
}
return 0;
}
运行结果:
1
3
5
7
9
11
13
15
17
19
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
printf(“%d\n”, arr[9]);
return 0;
}
运算结果:
```c
10
数组的使用
#include <stdio.h>
//打印数组里的每一个数
int main()
{
int n = 0;
int arr[10] = { 1,3,5,7,9,11,13,15,17,19 };
for (n = 0; n < 10; n++)
{
printf("%d\n", arr[n]);
}
return 0;
}
运行结果:
1
3
5
7
9
11
13
15
17
19