Kedog
Kedog

Reputation: 29

What is this code? And why is it working? C++

I know almost everything in this code except the last line. What is he doing after cout? And what is it called? My code for the same problem is almost thrice as big. And I was really shocked after I saw this. I'm new to c++ and it is my first time seeing something like this. I didn't even know that you can use parentheses as he did in his code. It would be nice if you answer noob friendly. Thank you

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    string c[]={"","one","two","three","four","five","six","seven","eight","nine"};
    cin>>a>>b;
    for(int i=a;i<=b;i++)
        cout<<((i<=9)?c[i]:((i%2==0)?"even":"odd"))<<endl;
}



Now my code

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int a;
int b;
cin >> a >> b;

for ( int n = a; n <= b; n++) {
     if ( n >= 1 && n <= 9 ) {

    string num;

    switch(n) {
    case 1: 
    num = "one";
    break;
    case 2: 
    num = "two";
    break;
    case 3: 
    num = "three";
    break;
    case 4: 
    num = "four";
    break;
    case 5: 
    num = "five";
    break;
    case 6: 
    num = "six";
    break;
    case 7: 
    num = "seven";
    break;
    case 8: 
    num = "eight";
    break;
    case 9: 
    num = "nine";
    break;
}
  cout << num << "\n" ;


    } else if (n > 9 && n % 2 == 0) {
    cout << "even \n" ; 
    } else {
    cout <<  "odd \n" ; }

}

    return 0;
}



Upvotes: 1

Views: 94

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 598174

?: is a ternary conditional operator:

The conditional operator expressions have the form

E1 ? E2 : E3

The first operand of the conditional operator is evaluated and contextually converted to bool. After both the value evaluation and all side effects of the first operand are completed, if the result was true, the second operand is evaluated. If the result was false, the third operand is evaluated.

So, in your example, this statement:

cout<<((i<=9)?c[i]:((i%2==0)?"even":"odd"))<<endl;

Has 2 nested ?: operators being used:

  • in the first one:

    E1 is (i<=9)
    E2 is c[i]
    E3 is ((i%2==0)?"even":"odd")

  • in the second one, from E3 above:

    E1 is (i%2==0)
    E2 is "even"
    E3 is "odd"

That statement is equivalent to this:

std::string temp; // because c[i] is a std::string...
if (i <= 9) {
    temp = c[i];
} else {
    if ((i % 2) == 0) {
        temp = "even";
    } else {
        temp = "odd";
    }
}
cout << temp << endl;

Upvotes: 0

UKL
UKL

Reputation: 31

This is called a ternary operator.

Upvotes: 0

tadman
tadman

Reputation: 211740

The original code can be made more understandable by breaking it up. First the includes:

#include <string>
#include <iostream>

Then define the number constants:

const std::string number[] ={"zero","one","two","three","four","five","six","seven","eight","nine"};

Then define a function that does what that ugly nested ternary does:

std::string nameOf(int v) {
  if (v < 10) {
    return number[v];
  }

  if (v % 2 == 0) {
    return "even";
  }

  return "odd";
}

Then the final code is actually really simple:

int main()
{
  int a,b;
  std::cin >> a >> b;

  for(int i=a; i<=b; ++i) {
    std::cout << nameOf(i) << std::endl;
  }

  return 0;
}

A ternary (X ? Y : Z) tests if expression X evaluates to a truthful value, and if it does then evaluates and returns expression Y. If not, expression Z is evaluated and returned.

This is different from a regular if which has no return value, but otherwise works the same way.

From a readability standpoint you want to keep your use of ternary operators to a minimum, they can get really confusing. Using a nested ternary is really uncalled for.

Upvotes: 3

Related Questions