当前位置:网站首页>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++){}请大家把答案发在评论区里。
边栏推荐
- MySQL transaction (transaction) (this is enough..)
- [MQ I can speak for an hour]
- Interviewer: If the order is not paid within 30 minutes, it will be automatically canceled. How to do this?
- 【JS面试题】面试官:“[1,2,3].map(parseInt)“ 输出结果是什么?答上来就算你通过面试
- Unity mobile game performance optimization series: performance tuning for the CPU side
- 如何将项目部署到服务器上(全套教程)
- DVWA安装教程(懂你的不懂·详细)
- MySQL优化:从十几秒优化到三百毫秒
- Apache DButils使用注意事项--with modifiers “public“
- MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
猜你喜欢

Sql解析转换之JSqlParse完整介绍

MySQL transaction (transaction) (this is enough..)

Blockbuster | foundation for platinum, gold, silver gave nameboards donors

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

centos7安装mysql5.7步骤(图解版)

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

Information System Project Manager Core Test Site (55) Configuration Manager (CMO) Work

MySQL_关于JSON数据的查询

Tapdata 与 Apache Doris 完成兼容性互认证,共建新一代数据架构

1. Get data - requests.get()
随机推荐
SQL row-column conversion
DVWA安装教程(懂你的不懂·详细)
SQL statement to range query time field
Simple read operation of EasyExcel
MySQL window function
Centos7 install mysql5.7
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)
Kubernetes加入集群的TOKEN值过期
MySQL database installation (detailed)
【mysql 提高查询效率】Mysql 数据库查询好慢问题解决
Linux系统安装mysql(rpm方式安装)
[mysql improves query efficiency] Mysql database query is slow to solve the problem
Go language study notes - dealing with timeout problems - Context usage | Go language from scratch
12 reasons for MySQL slow query
SQL injection of DVWA
MySQL optimization: from ten seconds to three hundred milliseconds
Three oj questions on leetcode
如何将项目部署到服务器上(全套教程)
.NET-6.WinForm2.NanUI learning and summary
MySQL忘记密码怎么办