#Wondering what the “?” and “:” characters do in C++

11 messages · Page 1 of 1 (latest)

strange plaza
#

I’m really new to C++, and I’m seeing notations where the “?” symbol is being used as what seems to be a conditional. For example:

int conditionalMultiplication (int a) {
return a%2 ? 9a : 8a
}

I am wondering what this does and how it can be used properly. In addition, I am wondering what a single colon means/represents.

Help would be greatly appreciated!

#

I’m sorry if the formatting of the code function is hard to read, I didn’t know how to paste the code directly from its source.

mighty crypt
#

!formatting

indigo marlinBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```py
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

mighty crypt
#

!formatting cpp

indigo marlinBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```cpp
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

stray dagger
#

It's essentially an if statement. It's called a ternary expression.

#

So

a ? b : c

Is the same as

if(a) {
    b;
}
else {
    c;
}
strange plaza
#

Oh ok, that clears things up. Thanks!

fast fox
stray dagger