90 min

Practice: conditions and decisions

Tom's 32 workbook questions on conditions: truth values and comparisons, single if, if...else, else-if chains, compound conditions with && || and !, tracing, and debugging. Every answer is hidden until you open it.

These are the 32 questions of Tom's workbook "Conditions and Decisions in C", with his numbering, so you can say "question 17" in class. The lessons behind them: True, false and comparisons, if, else and else-if and Combining conditions.

Allowed: relational operators, truth values, if, if...else, else-if chains, &&, ||, !, arithmetic, printf and scanf. Not yet: loops, switch, ?:, arrays and strings. Assume every input follows the stated rules; checking once is enough.

For every program: use int main(void), put #define _CRT_SECURE_NO_WARNINGS on line 1 when you use scanf in Visual Studio, and test below, at and above every boundary. Try each question before you open the answer.

Part A: Comparisons and truth values

1. Evaluate the comparisons

Write 0 or 1 for each expression.

8 > 3      8 == 3      5 <= 5      7 != 2
-1 >= 0    'A' == 'a'  10 < 4 + 7  6 % 2 == 0
Answer

Try it yourself first, then open the solution step by step.

    2. Translate rules into C expressions

    Write one C expression for each rule: age is at least 18; score is below 55; number is not zero; command is R; value is from 1 through 10 inclusive.

    Answer

    Try it yourself first, then open the solution step by step.

      3. Evaluate using stored values

      Given the declarations, write 0 or 1 for every expression.

      int x = 7;
      int y = 10;
      char command = 'Y';

      x < y, x + 3 == y, x != 7, command == 'y', x % 2 == 1

      Answer

      Try it yourself first, then open the solution step by step.

        4. Assignment versus equality

        Predict the final value of score and the exact output. Explain why the condition is true.

        int score = 40;
        if (score = 55)
        {
            printf("Matched\n");
        }
        printf("%d\n", score);
        Answer

        Try it yourself first, then open the solution step by step.

          5. Design boundary tests

          For each condition, give three useful test values and the expected truth value of each: x > 10; x >= 10; x == 0; x != 0.

          Answer

          Try it yourself first, then open the solution step by step.

            6. Repair a mathematical range

            Rewrite the expression as correct C. The rule is: score is from 0 through 100 inclusive.

            0 <= score <= 100
            Answer

            Try it yourself first, then open the solution step by step.

              Part B: Single if

              7. Multiple of five

              Read an integer. If it divides by 5, print Multiple of 5. Otherwise print nothing. Use one single if and %.

              Example: 10 prints Multiple of 5; 7 prints nothing.

              Answer

              Try it yourself first, then open the solution step by step.

                8. Nonzero using logical NOT

                Read an integer. Print Nonzero only when the value is not zero. Use one single if and logical NOT: !(number == 0).

                Example: -3 and 7 print Nonzero; 0 prints nothing.

                Answer

                Try it yourself first, then open the solution step by step.

                  9. High temperature

                  Read a double. Print High temperature only when the value is greater than 37.5. 37.5 itself is not high.

                  Example: 37.6 prints; 37.5 does not.

                  Answer

                  Try it yourself first, then open the solution step by step.

                    10. Uppercase character

                    Read one char. Print Uppercase only when it is from 'A' to 'Z' inclusive. Use two comparisons joined by &&.

                    Example: A and Z print; a does not.

                    Answer

                    Try it yourself first, then open the solution step by step.

                      Part C: if...else

                      11. Even or odd

                      Read an integer and print exactly Even or Odd. Use number % 2 == 0.

                      Example: 17 prints Odd.

                      Answer

                      Try it yourself first, then open the solution step by step.

                        12. Pass or fail

                        Read a score. A score of 55 or higher passes. Print Pass or Fail. Test 54, 55 and 56.

                        Answer

                        Try it yourself first, then open the solution step by step.

                          13. Adult or minor

                          Read an age. Print Adult when the age is at least 18, otherwise Minor. Test 17, 18 and 19.

                          Answer

                          Try it yourself first, then open the solution step by step.

                            14. Divisible or not

                            Read number and divisor. Print Divisible or Not divisible. Assume the divisor is not zero.

                            Example: 20 4 prints Divisible.

                            Answer

                            Try it yourself first, then open the solution step by step.

                              15. Freezing boundary

                              Read a temperature (double). Print Freezing or below when it is at most 0.0, otherwise Above freezing. Test -0.1, 0.0 and 0.1.

                              Answer

                              Try it yourself first, then open the solution step by step.

                                Part D: else-if chains

                                16. Classify a number by sign

                                Print Positive, Negative or Zero. Use one else-if chain.

                                Answer

                                Try it yourself first, then open the solution step by step.

                                  17. Classify a score

                                  Use these categories: 85 or higher Excellent; 70 to 84 Good; 55 to 69 Pass; below 55 Fail. Order the thresholds from the highest to the lowest.

                                  Example: 85 prints Excellent; 84 prints Good.

                                  Answer

                                  Try it yourself first, then open the solution step by step.

                                    18. Compare two integers

                                    Read two integers. Print First is larger, Second is larger or Equal. Use an else-if chain.

                                    Answer

                                    Try it yourself first, then open the solution step by step.

                                      19. Temperature categories

                                      Read a temperature (double): 30 or higher Hot; 20 up to 30 Warm; 10 up to 20 Cool; below 10 Cold. Use thresholds in descending order.

                                      Answer

                                      Try it yourself first, then open the solution step by step.

                                        20. Ticket category

                                        Read an age: below 6 Free; 6 to 17 Child; 18 to 64 Adult; 65 or higher Senior. Use an ordered else-if chain. Test 5, 6, 17, 18, 64 and 65.

                                        Answer

                                        Try it yourself first, then open the solution step by step.

                                          Part E: Compound conditions

                                          21. Valid score range

                                          Print Valid only for scores from 0 through 100 inclusive; otherwise print Invalid. Use &&. Test -1, 0, 100 and 101.

                                          Answer

                                          Try it yourself first, then open the solution step by step.

                                            22. Outside a range

                                            Print Outside when the value is below 10 or above 20; otherwise print Inside. Use ||. Test 9, 10, 20 and 21.

                                            Answer

                                            Try it yourself first, then open the solution step by step.

                                              23. Accept Y or y

                                              Read a char. Print Yes for 'Y' or 'y'; otherwise print No. Write the complete comparison on both sides of ||.

                                              Answer

                                              Try it yourself first, then open the solution step by step.

                                                24. Leap-year rule

                                                A year is a leap year when it divides by 400, or when it divides by 4 but not by 100. Print Leap year or Not a leap year. Use %, &&, || and !=.

                                                Example: 2000 leap; 1900 not; 2024 leap; 2023 not.

                                                Answer

                                                Try it yourself first, then open the solution step by step.

                                                  25. Discount

                                                  A customer gets a discount when the age is at least 65 or the membership is Y or y. Read the age (int) and the membership (char). Print Discount or Regular price. Use ||.

                                                  Answer

                                                  Try it yourself first, then open the solution step by step.

                                                    26. Safe division

                                                    Print Large quotient only when divisor is not zero and the integer number / divisor is greater than 5. Otherwise print Condition not satisfied. Put divisor != 0 on the left of &&.

                                                    Answer

                                                    Try it yourself first, then open the solution step by step.

                                                      Part F: Tracing and testing

                                                      27. Trace independent if statements

                                                      Predict the exact output and say which if owns the else.

                                                      int x = 7;
                                                      if (x > 0)
                                                          printf("A");
                                                      if (x < 10)
                                                          printf("B");
                                                      else
                                                          printf("C");
                                                      Answer

                                                      Try it yourself first, then open the solution step by step.

                                                        28. Trace an else-if chain

                                                        For the scores 92, 70, 55 and 54, say which conditions are checked and which word prints.

                                                        if (score >= 85)
                                                            printf("Excellent");
                                                        else if (score >= 70)
                                                            printf("Good");
                                                        else if (score >= 55)
                                                            printf("Pass");
                                                        else
                                                            printf("Fail");
                                                        Answer

                                                        Try it yourself first, then open the solution step by step.

                                                          29. Evaluate a compound condition

                                                          For x = -1, 0, 50 and 101, write the values of x >= 0, x <= 100 and the whole condition x >= 0 && x <= 100.

                                                          Answer

                                                          Try it yourself first, then open the solution step by step.

                                                            Part G: Debugging

                                                            30. Repair equality

                                                            The program should compare x with 10, but it changes x. Repair it.

                                                            if (x = 10)
                                                            {
                                                                printf("Ten\n");
                                                            }
                                                            Answer

                                                            Try it yourself first, then open the solution step by step.

                                                              31. Repair range validation

                                                              The program reports every integer as valid. Repair the logical operator.

                                                              if (x >= 0 || x <= 100)
                                                                  printf("Valid\n");
                                                              else
                                                                  printf("Invalid\n");
                                                              Answer

                                                              Try it yourself first, then open the solution step by step.

                                                                32. Repair alternative characters

                                                                The condition is always true. Repair it by writing a complete comparison on both sides of ||.

                                                                if (choice == 'Y' || 'y')
                                                                    printf("Yes\n");
                                                                Answer

                                                                Try it yourself first, then open the solution step by step.

                                                                  Before you hand in

                                                                  • The condition says exactly what the written rule says.
                                                                  • Each scanf and printf conversion matches the variable's type (%d int, %lf double when reading, %c char).
                                                                  • The categories of an else-if chain are in the right order.
                                                                  • Every compound condition has complete comparisons on both sides.
                                                                  • Your tests include values below, at and above every boundary.