Feeds:
Posts
Comments

Archive for January, 2016

I didn’t know in C++ you can writeĀ a code block, enclosed by ({}), as the condition of an if statement.

int main()
{
  if (({
    int sum = 0;
    for (int i =0; i < 1000; ++i) {
      sum += i;
    }
    sum == 500 * 999;
  })) {
  std::cout << "Correct!" << std::endl;
  }
  return 0;
}

Note that the last statement of the code block must be able to be evaluated to boolean value. Otherwise, the compiler will complain.

Advertisements

Read Full Post »