当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
Volatility取证工具使用日记
08 【生命周期 组件】
剑指offer基础版--- 第23天
什么是 GameFi?
04 【计算属性 侦听属性】
变量的解构赋值
C语言的文件操作(一)
基于web3.0使用钱包Metamask的三方登陆
Redis:简单实用
闭包(二)
剑指offer专项突击版 --- 第 3 天
mysql 的简单运用命令
PAT_乙级_真题练习_1007_素数对猜想
gin框架学习-Gin框架和Gorm框架搭建一个简单的API微服务
Three-party login using wallet Metamask based on web3.0
12 【nextTick 过渡与动画】
tf.keras.utils.get_file()
Redis first meeting
If the account number or password is entered incorrectly for many times, the account will be banned.