当前位置:网站首页>C language tutorial (3) - if and loop
C language tutorial (3) - if and loop
2022-07-31 05:47:00 【Why are so many names taken?】
Table of Contents
expression
Math expression
The value of a mathematical expression is a number.such as
1+2;2*3;4-4;5/2;3%2;
For mathematical expressions, 0 is false and non-0 is true.
Relational Expression
Relational expressions can only be one of two values, true and false.such as
1<2;//true 1<21>2; // false 1>21==2;//false 1=21!=2;//true 1≠21<=2;//true 1≤21>=2;//false 1≥2
Logical expression
&& or || or ! in logical expressions
If expression A&&expression B, if expression A is true, execute expression B; if expression A is false, the following does not need to be executed.This situation is called a short circuit.The entire expression is only true if both expression A and expression B are true.
If expression A||expression B, if expression A is true, expression B does not need to be executed, and the following does not need to be executed.This condition is called a short circuit.As long as one expression is true, the whole expression is true.
!: Negative operation.Turning true into false and false into true.
if
if
if can control the selection process, the usage is as follows:
if(expression){code}
If the expression is true, execute the code inside the {} (compound statement).
if else
The usage of if else is as follows:
if(expression){code 1}else{code 2}
If the expression is true, execute code 1, otherwise execute code 2.
loop
How to calculate the sum of the numbers from 0 to 10 in C language?
while
The usage of while is as follows:
while(expression){code}
If the expression is true, loop through the code.Each time through the loop, check whether the expression is true or not.
How to use while to write the above question?The code is as follows:
#includeint main(void){int i=0, num=0;while(i<=10){num += i; //num += i is num = num + ii++; // i++ is i+=1}printf("0+1+2+...+10 is %d\n", num);return 0;}
Results:
0+1+2+...+10 is 55
for
The usage of for is as follows:
for(expression 1 (can not write); expression 2 (can not write); expression 3 (can not write)){code}
Expression 1 is executed once before the loop; if expression 2 is true, the loop executes the code.Once in each loop, check whether expression 2 is true.Expression 3 represents the code to be executed after each loop.
How to use for to write the above question?The code is as follows:
#includeint main(void){int i, num=0;for(i=0;i<=10;i++){num += i; //num += i is num = num + i}printf("0+1+2+...+10 is %d\n", num);return 0;}
Results:
0+1+2+...+10 is 55
A problem:
Is the following loop an infinite loop?
short i;for(i=0; i>-1; i++){}
Please post your answers in the comments section.
边栏推荐
猜你喜欢
Redis:安装使用
什么是 GameFi?
剑指offer专项突击版 --- 第 4 天
【C语言趣味小游戏——猜数字】
第7章 网络层第2次练习题答案(第三版)
代码块、Package,Import,封装(第六天)
uni-app进阶之认证【day12】
The process and specific code of sending SMS verification code using flask framework
gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
If the account number or password is entered incorrectly for many times, the account will be banned.
随机推荐
uni-app进阶之内嵌应用【day14】
剑指offer基础版 ----第31天
gin框架学习-Casbin入门指南(ACL、RBAC、域内RBAC模型)
数字孪生将成为进入“元宇宙”一项重要的途径
sqlmap注入教程 常用指令
Quickly master concurrent programming --- the basics
剑指offer基础版 ---- 第27天
运用flask框架发送短信验证码的流程及具体代码
win11中利用IIS10搭建asp网站
C语言的文件操作(一)
vulhub靶场学习日记hackme1
leetcode-每日一题735. 行星碰撞(栈模拟)
字符串的新增方法
局部变量成员变量、引用类型、this,static(第五天)
什么是 GameFi?
三次握手与四次挥手
C语言实验五 循环结构程序设计(二)
C语言文件读、写、定位函数
Object Detection Study Notes
剑指offer基础版--- 第23天