当前位置:网站首页>【HNUMSC】C language second lecture
【HNUMSC】C language second lecture
2022-08-09 02:07:00 【Do bionic programmers dream of electric sheep?】
CLanguage Lesson 2
循环
A loop is the repeated execution of other statements(循环体)的语句.
CThe language provides three repetitions:
- while
- do
- for
while语句
while(i<n){
i=i+2;
}
int i=5;
while(i>=1){
printf("%d\n",i);
i--;
}
do语句
do语句本质上就是while语句,It's just that the control expression is evaluated after each execution of the loop body.
do语句while(表达式);
int i =10;
do{
printf("%d\n",i);
i--;
}while(i>0);
for语句
forStatements are very suitable for application in use“计数”variable in a loop.
for语句格式:
for(表达式1;表达式2;表达式3)语句
从0向上加到n-1:
int i;
for(i=0;i<10;i++)
printf("i=%d\n",i);
执行for语句时,变量i先初始化为0,Then decidei是否小于10.Because the result of the judgment is true,所以打印i=0,然后对变量i进行自增操作.Then again on the conditioni>0进行判定,直到不满足条件(判定为false)为止.
forthe omitted expression
CThe language allows omitting any or all expressions:
int i=0;
for(;i<10;i++)
printf("i=%d\n",i);
int i;
for(i=0;i<=10;){
printf("i=%d\n",i);
i++;
}
如果省略表达式2,那么它默认为真值,for语句不会终止,会无限循环.
for语句声明
int i=2;
for(int i=0;i<10;i++)
printf("i=%d\n",i);
printf("%d",i);
for语句声明的变量不可以在循环外访问.
循环中的break语句
int main(void)
{
int i=0;
for(;;i++){
printf("i=%d\n",i);
if(i==5){
break;
}
}
return 0;
}
continue语句
breakstatement causes the program to break out of the loop,而continue语句会把程序控制留在循环内.
//The loop aborts after ten non-zero numbers are read
int n=0;
int sum=0;
while(n<0){
scanf("%d",&i);
if(i==0)
continue;//Skip read0
sum +=1;
n++;
}
基本类型
整数类型
- int
- long int
整数溢出
When performing arithmetic operations on integers,The result may be too large to represent.
例如,对两个intvalue when performing arithmetic operations,结果必须仍然能用int类型来表示,否则会发生溢出.
long l;
scanf("%ld",&l);
printf("%ld",l);
浮点类型
C语言提供3种浮点类型,对应三种不同的浮点格式
- float:单精度浮点数
- double:双精度浮点数
- long double:扩展精度浮点数
double d;
scanf("%lf",&d);
//printfin function formatf可以用来写double类型(不能用lf)
字符类型
char类型,That is, the character type
ASCII码
charVariables of type can be assigned any single character:
char ch;
ch ='a';
ch ='A';
ch ='0';
ch =' ';//空格
ch =ch+1;
可以像比较数那样对字符进行比较
下面的if语句测试ch中是否含有小写字母,如果有,那么它会把chConvert to the corresponding capital letters.
int main(void)
{
char ch;
ch='f';
if ('a'<=ch && ch<='z')
ch =ch -'a'+'A';
printf("%c",ch);
return 0;
}
getchar
可以使用getchar函数和putcharfunction to read and write characters.
putcharfunction for writing单个字符
每次调用getchar函数时,它会读入一个字符并将其返回.
ch=getchar();//to save this character,It must be stored in a variable using an assignment operation
数组
一维数组
声明数组a有10个int类型的元素:
int a[10];
Or define it with a macro:
#define N 10
int a[N];
数组下标
To access specific array elements,You can write an array name followed by an integer value surrounded by square brackets(对数组取下标或进行索引)
如果a是含有10个元素的数组,Can be marked as sucha[0],a[1]…a[9]
数组索引从0开始
数列反向
数组初始化
对数组使用sizeof运算符
多维数组
二维数组m有5行9列:
int m[5][9];
结构体
边栏推荐
猜你喜欢

2.1-----27. Remove elements

LeetCode每日两题01:二分查找 (均1200道)

How SEMRush finds keywords for advertising

HNUMSC-C语言第一课

pytorch相关知识点总结

Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules

Data recovery software EasyRecovery supports recovery of all types of files

德语翻译器在线翻译中文

torchversion.transforms的使用

New Swagger3.0 tutorial, OAS3 quick configuration guide, to automate API interface documentation!
随机推荐
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
帮助安全红队取得成功的11条建议
JDBC技术(三)——使用Druid数据库连接池测试
数字孪生+燃气管理,开启智慧燃气管理新模式
The most fierce "employee" in history, madly complaining about the billionaire boss Xiao Zha: So rich, he always wears the same clothes!
D. Tournament Countdown
pytorch相关知识点总结
如何在群晖系统中安装cpolar(群晖6.X版)
Electromagnetic radiation safety standards and detection methods
企业从云服务的承诺支出中获得最大收益的四种方法
webrtc 编译
2020.10.13开发日志
谷歌翻译下载-免费谷歌翻译软件下载
增额终身寿险哪家最好呢?真的安全吗?
LeetCode每日两题01:二分查找 (均1200道)
What is the difference between a project manager and a product manager?
2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
D. Tournament Countdown
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
智能视频监控设计摄像头部分