C-Programmierung

Kontrollstruktur If Beispiele

Kontrollstruktur If-Else Flussdiagramm | | Kontrollstruktur If-If

Vormittags und nachmittags

/* ask for hour input */
printf("input hour (0-24): ");
scanf("%d", &h);

/* check hour range */
if (h<0 || h>24)
   printf("warning: out of range\n");

/* check pm range */
if (h>=12)
   printf("pm\n");
else
   printf("am\n");

Bool’sche Ausdrücke

if (x%2==0) printf("x is even");
if (x%2) printf("x is odd");
if ((x>>(n-1)) & 1) printf("nth Bit set in x");

Fallgruben:

if (counter = 0) /* assignment instead of comparison operator */
   printf("never reached");

if (counter == 0)
   printf("ok");
unsigned int x=0;
if (! x>0) /* higher priority of ! operator */
   printf("never reached");

if (! (x>0))
   printf("ok");

if (x<=0)
   printf("ok");


Kontrollstruktur If-Else Flussdiagramm | | Kontrollstruktur If-If

Options: