[Solved] do some statement under a condition without using a single if [closed]


Yes, it is doable (with some restrictions).

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// Assuming 2s-complement numbers
// and an understanding compiler
// Some checks omitted!

int main(int argc, char **argv)
{
  int input;
  unsigned int itmp;
  int size;
  int sign, sin, sout;
  char out[3] = { '0', '+', '-' };


  if (argc != 2) {
    fprintf(stderr, "Usage: %s integer\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  // TODO: use strtol and check input!
  input = atoi(argv[1]);

  size = sizeof(int) * CHAR_BIT;
  itmp = (unsigned int) input;

  sin = itmp >> (size - 1);
  sign = sin ^ 1;

  // now "sign" is either 0 (negative) or 1 (positive)
  // but we need 1 (negative) and -1 (positive)
  // 0 * -2 + 1 = 1
  // 1 * -2 + 1 = -1

  sign = sign * -2 + 1;

  // Now we can do
  // in   sign   out   sin   sout
  // -x *   1 = -x ->   1  +  1  =  2
  // +x *  -1 = -x ->   0  +  1  =  1
  //  0 *  -1 =  0 ->   0  +  0  =  0

  itmp = itmp * sign;
  sout = itmp >> (size - 1);

  sign = sin + sout;

  printf("Input: %c\n", out[(size_t) sign]);

  exit(EXIT_SUCCESS);
}

No conditional expressions, neither explicit with if, nor implicit with while or for, nor the shortcut ...?...:....

solved do some statement under a condition without using a single if [closed]