当前位置:网站首页>C语言教程(三)-if和循环
C语言教程(三)-if和循环
2022-07-31 05:09:00 【怎么这么多名字都被占了】
目录
表达式
数学表达式
数学表达式的值为数。如
1+2;
2*3;
4-4;
5/2;
3%2;对于数学表达式,0为假,非0为真。
关系表达式
关系表达式只能是两个值之一,即真和假。如
1<2;//真 1<2
1>2;//假 1>2
1==2;//假 1=2
1!=2;//真 1≠2
1<=2;//真 1≤2
1>=2;//假 1≥2逻辑表达式
逻辑表达式中有&&或||或!
若 表达式A&&表达式B,如果表达式A为真,执行表达式B;如果表达式A为假,后面的就不用执行了。这个情况称之为短路。如果表达式A和表达式B全部为真,整个表达式才为真。
若 表达式A||表达式B,如果表达式A为真,就不用执行表达式B,后面的就不用执行了。这种情况称之为短路。只要有一个表达式为真,整个表达式就为真了。
!:取反操作。即将真变为假,假变为真。
if
if
if可以进行选择流程控制,使用方法如下:
if(表达式){
代码
}如果表达式为真,则执行{}(复合语句)里的代码。
if else
if else的用法如下:
if(表达式){
代码1
}else{
代码2
}如果表达式为真,则执行代码1,否则执行代码2。
循环
如何用C语言计算0-10的各数之和?
while
while的用法如下:
while(表达式){
代码
}若表达式为真,则循环执行代码。每循环一次,判断一次表达式是否为真。
如何用while来写前面说的问题?代码如下:
#include<stdio.h>
int main(void){
int i=0, num=0;
while(i<=10){
num += i; //num += i就是num = num + i
i++; // i++就是i+=1
}
printf("0+1+2+...+10 is %d\n", num);
return 0;
}
结果:
0+1+2+...+10 is 55for
for的用法如下:
for(表达式1(可以不写);表达式2(可以不写);表达式3(可以不写)){
代码
}表达式1在循环之前执行一次;若表达式2为真,则循环执行代码。每循环一次,判断一次表达式2是否为真。表达式3表示每循环完一次后执行的代码。
如何用for来写前面说的问题?代码如下:
#include<stdio.h>
int main(void){
int i, num=0;
for(i=0;i<=10;i++){
num += i; //num += i就是num = num + i
}
printf("0+1+2+...+10 is %d\n", num);
return 0;
}结果:
0+1+2+...+10 is 55出个问题:
以下循环是不是死循环?
short i;
for(i=0; i>-1; i++){}请大家把答案发在评论区里。
边栏推荐
- The 15th day of the special assault version of the sword offer
- SQL语句中对时间字段进行区间查询
- tf.keras.utils.get_file()
- Summary of MySQL common interview questions (recommended collection!!!)
- SQL statement to range query time field
- Moment Pool Cloud quickly installs packages such as torch-sparse and torch-geometric
- .NET-9. A mess of theoretical notes (concepts, ideas)
- Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
- 如何将项目部署到服务器上(全套教程)
- About the problems encountered by Xiaobai installing nodejs (npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
猜你喜欢

MySQL-Explain详解

【mysql 提高查询效率】Mysql 数据库查询好慢问题解决

面试官,不要再问我三次握手和四次挥手

关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)

The interviewer asked me TCP three handshake and four wave, I really

MySQL database installation (detailed)

MySQL-如何分库分表?一看就懂

Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?

Go language study notes - dealing with timeout problems - Context usage | Go language from scratch

Kubernetes 证书可用年限修改
随机推荐
MySQL优化之慢日志查询
MySQL8--Windows下使用压缩包安装的方法
MySQL事务隔离级别详解
Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构
Redis Advanced - Cache Issues: Consistency, Penetration, Penetration, Avalanche, Pollution, etc.
Interview Redis High Reliability | Master-Slave Mode, Sentinel Mode, Cluster Cluster Mode
Centos7 install mysql5.7
ABC D - Distinct Trio (Number of k-tuples
MySQL optimization: from ten seconds to three hundred milliseconds
Centos7 install mysql5.7 steps (graphical version)
Temporal客户端模型
分布式事务处理方案大 PK!
MySQL事务(transaction) (有这篇就足够了..)
Flink sink redis 写入Redis
TOGAF之架构标准规范(一)
再见了繁琐的Excel,掌握数据分析处理技术就靠它了
SQL row-column conversion
精解四大集合框架:List 核心知识总结
工作流编排引擎-Temporal
matlab abel变换图片处理