New to C++, question about a code

I'm learning it on my own while I'm out here on deployment, and came across this problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  // Write a single 'if' statement that examines two integer variables and changes the 
// larger to the smaller, using only one else clause

#include <iostream>
int main()
{
    using namespace std;
    
    int x, y;
    cout << "Enter a value for x: ";
    cin >> x;
    cout << "\nEnter a value for y: ";
    cin >> y;
    
    if (x > y)
       x = y;
    else
        y = x;
        
    cout << "\n\nAfter evaluation, the values of the two variables are now x: " << x << " and y: " << y;
    
    char response;
    cin >> response;
    return 0;
}


This same section I got the question from also covers ternary operators ( ?: ).

My question is, using the ternary or any other operator, is the a more concise way to right the code above?

If there is, I'd like to know why it's better, aside from just personal preferences. Thanks!
Yep lines 15-18 could be replaced with one line using the ternary operator.
It would look more concise i guess, but take a little longer for someone to understand what it's doing.
Is it better? I think it's a personal preference thing. However, in my last company it was against the company's coding standards to use the ternary operator as it was deemed a little unclear to someone reading the code.
I would agree with mutexe. If you don't like having functions take up a lot of space, than ternary operators can solve that. Me, I like if/else statements. It is just easier. That and I do a bit of Unity (game engine, assuming you've heard of it) and you use a lot of if statements and such for AI.

Also, you said you were out on deployment, so I am guessing you are military. If so, then thanks for serving the country! If not, then I am completely wrong and misinterpreted that.
Thanks guys, I'll try re-writing this with the ternary op just to see how it feels. I am thinking that using if/else would be better for me, for simple readability.

And to answer your question, Ace, yes, I'm military!
Topic archived. No new replies allowed.