LateStart
LateStart

Reputation: 1

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

Upvotes: 0

Views: 113

Answers (3)

Mohamed Ahmed
Mohamed Ahmed

Reputation: 11

J-=1<3 is a boolean expression . which means j=j - 1<3 . here the compiler will examine the comparison between 1 and 3 which is true . true means in c++ 1 so j=j - 1 . this expression is the same as j- -

Upvotes: -1

Some programmer dude
Some programmer dude

Reputation: 409166

The expression i < 3 is a boolean expression. It's either true or false

In C++ true and false are implicitly convertible to the int values 1 and 0 (respectively).

So depending on the value of i it's either equal to

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

or

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

Upvotes: 4

Paul R
Paul R

Reputation: 212949

It’s just a more terse way of writing:

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

Upvotes: 2

Related Questions