Computer Systems Fundamentals

#include <stdio.h>
#define N 10

int main(void){
   int a[] = {4, 3, 9, -8, 5, -4, 3, 1, 0, 4};
   int sum = 0;
   int i;
   for (i = 0; i < N; i++) {
       if (a[i] == 0) break;
       if (a[i] < 0) continue;
       sum += a[i];
   }
   printf("The sum is %d\n",sum);
   return 0;
}
#include <stdio.h>
#define N 10

int main(void){
   int a[] = {4, 3, 9, -8, 5, -4, 3, 1, 0, 4};
   int sum = 0;
   int i;
   for (i = 0; i < N && a[i] != 0; i++) {
       if (a[i] > 0) {
           sum += a[i];
       }
   }

   printf("The sum is %d\n",sum);
   return 0;
}
#include <stdio.h>

// Correct version

int main(void){
    int ch;              
    while ((ch = getchar()) != EOF ) {
        printf("Got %d %c\n",ch,ch);
    }

    return 0;
}


// InCorrect version
/*
int main(void){
    int ch;
                     
    while (ch = getchar() != EOF) {
         printf("Got %d %c\n",ch,ch);
    }
    return 0;
}
*/
// (a)
if (x > 0)
   y = x - 1;
else
   y = x + 1;

y = (x > 0) ? x-1 : x+1


// (b)
if (x > 0)
   y = x - 1;
else
   z = x + 1;


Can't use conditional expr
Different vars in each branch of if

// (c)
if (x > 0) {
   y = x - 1;  z = x + 1;
} else {
   y = x + 1;  z = x - 1;
}

y = (x > 0) ? x-1 : x+1;
z = (x > 0) ? x+1 : x-1;