Skip to main content
Logo image

Dive Into Systems: Exercises

Section 1.3 Conditionals and Loops

Exercise 1.3.1. Simple Conditionals.

Given this code, what does it print to the screen?
int x;

x = 10;
if (x < 10) {
    printf("Sharks\n");
} else {
    printf("Jets\n");
}

Exercise 1.3.2. if-else-if-else chains.

Given this code, what does it print to the screen?
int x;
x = 10; 

if (x < 10) { 
    printf("Apple");
} else if (x < 20) {
    printf("Banana");
} else if (x < 30) {
    printf("Cherry");
} else {
    printf("Durian");
}

Exercise 1.3.3. Debug Conditionals.

    What is wrong with this code? It is supposed to print the smallest factor among 2, 3, or 5 of the number typed in (the program prints nothing if the number entered is not evenly divisible by 2, 3 or 5).
    int x;
    
    scanf("%d",&x);
    if (x % 2 == 0) {
      printf("x is divisible by 2\n");
    } else if (!(x % 3)) {
      printf("x is divisible by 3\n");
    } else (x % 5 == 0) {
      printf("x is divisible by 5\n");
    }
    
  • Else blocks cannot have a conditional attached to them
  • Correct!
  • (x % 5) does not properly check for divisibility by 5
  • Incorrect.
  • You have to check for divisibility in order, i.e. checking for 2 before checking for 3 or 5
  • Incorrect.
  • To check for divisibility by 2, you have to say "else (x % 2)" instead.
  • Incorrect.
Hint.
Else blocks always happen if the earlier conditionals are all false

Exercise 1.3.4. Logic Operators.

    What will this code output if the user types in 0?
    int x;
    
    scanf("%d",&x);
    if (!x) {
        printf("Apple\n");
    } else if (x > 0) {
        printf("Banana\n");
    } else if (x < 0) {
        printf("Cherry\n");
    } else {
        printf("Dragonfruit\n");
    }
    
  • Apple
  • Correct
  • Banana
  • Incorrect
  • Cherry
  • Incorrect
  • Dragonfruit
  • Incorrect
Hint 1.
What number means false in C?
Hint 2.
What does the ! operator do?

Exercise 1.3.5. Complex Conditionals.

    Given the following code:
    int temp, humidity;
    
    scanf("%d", &temp); 
    scanf("%d", &humidity);
    
    if (cond) {
        printf("The weather looks good today!\n");
    }
    
    Which of the following options for cond will correctly print out “The weather looks good today!” if (and only if) the temperature is in the range 60 to 80 or the humidity is in the range 30 to 50?
  • ((humidity >= 30) && (humidity <= 50)) || ((temp >= 60) && (temp <= 80))
  • Correct! This is one way of writing the complex conditional described in the problem.
  • ((temp >= 60) || (temp <= 80)) && ((humidity >= 30) || (humidity <= 50))
  • Incorrect. Mind your ORs and ANDs!
  • ((temp >= 60) && (temp <= 80)) && ((humidity >= 30) && (humidity <= 50))
  • Re-read the question carefully. Should there be an OR in there?
  • ((temp >= 60) && (temp <= 80)) || ((humidity >= 30) && (humidity <= 50))
  • Correct! This is one way of writing the complex conditional described in the problem.
Hint.
The ranges are inclusive, meaning that 60 and 80 are ok for the temperature, and 30 and 50 are both ok for the humidity. Remember the && operator means "and".

Exercise 1.3.6. For Loops (Q1).

How many times does this loop print "Hello"? `
int i;
    
for (i = 1; i < 5; i++) {
    printf("Hello\n");
}

Exercise 1.3.7. For Loops (Q2).

How many times does this loop print "Hello"? `
int i;
int x;

x = 7;
for (i = 0; i < x; i++) {
    printf("Hello\n");
}

Exercise 1.3.8. For Loops (Q3).

How many times does this loop print "Hello"? `
int i;

for (i = 0; i < -10; i++) {
    printf("Hello\n");
}

Exercise 1.3.9. For Loops (Q4).

How many times does this loop print "Hello"? `
int i;

for (i = 1; i < 9; i+=2) {
    printf("Hello\n");
}

Exercise 1.3.10. For Loops (Q5).

How many times does this loop print "Hello"? `
int i;

for (i = 5; i >= 0; i--) {
    printf("Hello\n");
}

Exercise 1.3.11. While Loops (Q1).

How many times does this loop print "tweet"? `
int i;

i = 3;
while (i > 0) {
   printf("tweet\n");
   i--;
}

Exercise 1.3.12. Practice Writing C.

Write a loop that will print out the numbers from 100 to 200 coutning by 5s.
Answer.
int i;
for (i = 100; i <= 200; i+=5){
    printf("%d\n", i);
}

Exercise 1.3.13. For Loops (Debugging).

    What is wrong with the following code? It is supposed to print moo ten times on separate lines.
    int i;
       
    for (i = 0; i < 10; i--) {
        printf("moo\n");
    }
    
  • It prints "moo" fewer than 10 times.
  • Incorrect.
  • It prints "moo" more than 10 times.
  • Correct! This seems like an infinite loop, but in fact, 10 is printed many many times. Since i is signed, it will cycle through all the negative numbers, and perhaps underflow to MAX_INT. You will learn more about underflow in Chapter 4.
  • It does not print anything at all.
  • Oh it prints something, alright!
  • It causes an infinite loop.
  • Actually, this loop terminates.
  • This code will not compile.
  • This is valid C code!
Hint 1.
i is a signed number. Think about what that can mean!
Hint 2.
The actual number of times that this code executes is hard to predict, since the behavior can change based on compiler. However, one of the above conditions are definitely true!

Exercise 1.3.14. True/False.

    True or False: The following code always prints 0 through 9.
    int i;
       
    while (i < 10) {
        printf("%d\n",i);
        i++;
    }
    
  • True.

  • \(i\) is uninitialized, meaning there is no guarantee how many times the loop will run. This is why it is important to initialize variables before using them.
  • False.

  • \(i\) is uninitialized, meaning there is no guarantee how many times the loop will run. This is why it is important to initialize variables before using them.
Hint.
The variable \(i\) is unitialized. How does that impact things?

Exercise 1.3.15. Debugging.

    What is wrong with the following code? It is supposed to print the numbers 0 through 9.
    int love;
       
    love = 0;
    while (love < 10) {
        printf("%d\n", love);
    }
    
  • This code does not compile.
  • Actually it does!
  • This code does not print anything at all.
  • Take a look again at this loop. There is no prohibitions agianst it running!
  • This code contains an infinite loop.
  • This loop is missing an increment step. As such, the loop will print 0 forever.
  • This code correctly executes 10 times.
  • Incorrect!.
Hint.
Consider the components of a while loop.

Exercise 1.3.16. Parsons Problem: Converting For Loops to While Loops.

Rewrite the following for loop as a while loop.
int i;
   
for (i = 0; i < 10; i++) {
    printf("Hello\n");
}
The code blocks below can be rearranged to form one of the many possible programs to implement this algorithm to compute a list of all the primes less than \(250\text{.}\) [Ed. this version of this problem requires the reader to provide the necessary indentation.]

Exercise 1.3.17. Parsons Problem: Converting While Loops to For Loops.

Rewrite the following while loop as a for loop.
int i;
   
i = 5;
while (i < 10) {
    printf("yay!\n");
    i = i + 2;
}

Exercise 1.3.18. True/False.

    These two blocks of code print the same thing.
    int x, y;
       
    x = 10;
    y = 20;
    
    if ((x >= 10) && (y < 30)) {
        printf("roar\n");
        y++;
    }
    
    int x, y;
       
    x = 10;
    y = 20;
    
    while ((x >= 10) && (y < 30)) {
        printf("roar\n");
        y++;
    }
    
  • True.

  • False. The if statement runs one time, printing Hello once. The while loop runs 10 times, printing Hello 10 times.
  • False.

  • False. The if statement runs one time, printing Hello once. The while loop runs 10 times, printing Hello 10 times.
Hint.
While the two code blocks look nearly identical, there is something different about the conditional block. What is the implication of if vs while?