简体   繁体   English

C ++语法-条件运算符和算术运算符的组合

[英]C++ syntax - combination of conditional and arithmetic operators

I have encountered in some algorithm shortened syntax for the first time: 我第一次遇到某种算法缩短的语法:

j -= i < 3;

Can you please explain to me what it means 你能告诉我这是什么意思吗

The expression i < 3 is a boolean expression. 表达式i < 3是布尔表达式。 It's either true or false 这是true还是false

In C++ true and false are implicitly convertible to the int values 1 and 0 (respectively). 在C ++中, truefalse分别隐式转换为int10

So depending on the value of i it's either equal to 所以根据i的值等于

j -= 1;  // i < 3 is true

or 要么

j -= 0;  // i < 3 is false

It's just a more terse way of writing: 这只是一种更简洁的书写方式:

if (i < 3)
    j = j - 1;

J-=1<3 is a boolean expression . J- = 1 <3是布尔表达式。 which means j=j - 1<3 . 这意味着j = j-1 <3。 here the compiler will examine the comparison between 1 and 3 which is true . 在这里,编译器将检查1和3之间的比较,这是正确的。 true means in c++ 1 so j=j - 1 . true在c ++ 1中表示j = j-1。 this expression is the same as j- - 此表达式与j-相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM