当前位置:网站首页>【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];
结构体
边栏推荐
- pytorch相关知识点总结
- How js implements array deduplication (7 kinds)
- Significance Test--Study Notes
- Several ways to use JS to achieve array flattening
- 使用百度EasyDL实现智能垃圾箱
- Educational Codeforces Round 132 (Rated for Div. 2)
- 2022 China Eye Expo, China Beijing International Children and Adolescent Eye Health Industry Exhibition
- composer的使用记录
- HNUMSC-C语言第一课
- D. Tournament Countdown
猜你喜欢
力扣刷题记录6.1-----203. 移除链表元素
Likou Brush Question Record 8.1-----206. Reverse linked list
MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions
Educational Codeforces Round 132 (Rated for Div. 2)
JDBC技术(三)——使用Druid数据库连接池测试
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
How to install ngrok in Synology system (Synology 6.X version)
class path resource [bean.xml] cannot be opened because it does not 错误解决方案
PostMan import certificate add certificate
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
随机推荐
数据库设计的总结
TP测试查询数据库字段为null或空的字段
Composer usage record
软件测试技术之如何编写测试用例(5)
etcd实现大规模服务治理应用实战
Z-Game on grid
右键新建缺少word、excel选项问题处理
帮助安全红队取得成功的11条建议
Go-11 - Process Control
JDBC technology (3) - use Druid database connection pool test
Go-12-结构体
《LC刷题总结》——贪心
webrtc 编译
Electromagnetic radiation safety standards and detection methods
2.1-----27. Remove elements
Wireshark packet capture tool
德语翻译器在线翻译中文
配置文件的读取-TOML
LeetCode每日两题01:二分查找 (均1200道)
低代码开发创新企业应用构建模式