Control and Logical structures

True and False

In C truth is very simple.

  • 0 is False
  • Anything else is true

Logical operators

!  - not -      15
&& - Logical and 5
|| - Logical or  4

Short Circuit evaluation

int x = 5;
int y = 2;

(x + y) || ++x // returns 1 and x isnt updated
!(x+y)  || ++x // returns 1 and x is updated
int x = 2, y z;
printf("\n%d\n", 1 > 5 || 6 < 50 && ++x < 5);
printf("\n\n%d\n\n",x)
printf("\n\n\%d\n\n",

 14 == 16 || !(++x < 29) && ++x < 52);

printf("\n\n%d\n\n",x )

Ternary operator

a == b ? c -- : c+++

printf("%s", (sales > 100.0) ? "Federal Express." : "U.S Mail.");

Switch

switch (/* expression */) {
  case /* value */: statement
    break;
  case /*value*./: statement
    break;
  default : statement
}
Written on September 18, 2017