Conditionals


int main(void) {

  int foo;

  //EXAMPLE One
  if (foo > 10) {
    printf("Condition one");
  } else {
    printf("Condition two");
  }



  //EXAMPLE two
  if (foo > 10 || foo < 20) {
    printf("Condition one")
  } else if (foo < 30){
    printf("Condition two")
  } else {
    print("Condition three")
  }



  //Example Three
  if ((foo > 10) && (foo % 5) == 0) {
    printf("Condition One");
  } else if ((foo % 5) == 0) {
    printf("Condition Two")
  } else {
    print("Condition three")
  }

  return 0;
}

Pointers

int foo;
int *p;
int *bar;

foo = 10;
p = &foo;
bar = p;

//EXAMPLE four
if (*p == 10) {
  printf("Condition one")
} else {
  printf("Condition two")
}

//EXAMPLE five
if (&foo == bar) {
  printf("Condition one")
} else {
  printf("Condition two")
}

//Example six
foo += 20;
*bar -= 5;
*p += 10;

if (*p == 10) {
  printf("Condition one")
} else if (foo == 30) {
  printf("Condition two")
} else {
  printf("Condition three")
}

Arrays

//Example Seven
int n = 10;
int a[n];

for(int i = 0; i < n; i++) {
  a[i] = 100 + i;
}

for (int i = 0; i < n; i++)
   printf("%d\n", a[i]);
//Example Eight
#DEFINE len(x) = sizeof(x) / sizeof(x[0])

int a[10] = {7, 5, 4, 3, 9, 100, 5, 8, 1, 6};
int m = a[0];

for (int i = 0; i < len(a); i++) {
   if a[i] > m {
     m = a[i]
   }
}

printf("%d\n", m);
//Example Nine
#DEFINE len(x) = sizeof(x) / sizeof(x[0])

repeat = 3;
int a[10] = {7, 5, 4, 3, 9, 100, 5, 8, 1, 6};
int b[repeat * len(a)];

for(int i = 0; i < len(b); i++) {
  for(int j = 0; j < repeat; j++) {
    b[i + j * len(a)] = a[i];
  }
}

for(int i = 0; i < len(b); i++) {
  printf("%d\n", b[i]);  
}

Review Lec 8:

Question 1
What is the numeric value of the expression 7 < 9?


Question 2
Under what conditions will this code print "water"?

if(T < 32)
    printf("ice\n");
else if(T < 212)
    printf("water\n");
else
  printf("steam\n");


Question 3
What would this code print?

int x = 3;
if(x)
  printf("yes\n");
else
  printf("no\n");


Question 4
What do these loops print?

//loop 1
for(i = 0; i < 10; i = i + 2)
    printf("%d\n", i);

//loop 2
for(i = 100; i >= 0; i = i - 7)
    printf("%d\n", i);

//loop 3
for(i = 1; i <= 10; i = i + 1)
    printf("%d\n", i);

//loop 4
for(i = 2; i < 100; i = i * 2)
    printf("%d\n", i);


Question 5
What is printed?

int i;

for(i = 0; i < 3; i = i + 1)
  printf("a\n");
  printf("b\n");

printf("c\n");


Question 6
What is printed?

#include <stdio.h>

int main()
{
    int i, j;
    for(i = 1; i <= 10; i = i + 1) {
        for(j = 1; j <= 10; j = j + 1) {
            if((i + j) % 2 == 0)
                printf("* ");
            else
        printf("  ");
        }
        printf("\n");
    }
    return 0;
}


Question 7
How many elements does the array int a[5]; contain?


Question 8
What's wrong with this scrap of code?

int a[5];
for(i = 1; i <= 5; i = i + 1)
    a[i] = 0;


Questin 9
What does the function does_something do? What is printed by this program?

#include <stdio.h>

int a[] = {1, 2, 3, 4, 5, 6};

int does_something(int [], int);

int main()
{
    printf("%d\n", does_something(a, 6));
    return 0;
}

int does_something(int a[], int n)
{
    int i;
    int foo = 0;
    for(i = 0; i < n; i = i + 1)
        foo = foo + a[i];
    return foo;
}