If/Else Decisions Challenges
| 4 challenges |
Week 4 |
Week 4 (C Control Flow, Functions, and Writing Real Programs) |
Exercises covering conditional logic including assertions, boolean/logical expressions, if/else/else-if chains, and debugging conditional code in C.
Difficulty Breakdown
| Level |
Count |
| Beginner |
3 |
| Intermediate |
1 |
Challenges
1. Assertions1
| Difficulty: Intermediate |
Time: ~15 min |
Type: assertion table |
For each of the five points labeled by comments, identify each of the assertions in the table below as either being always true, never true, or sometimes true / sometimes false.
Concepts: program state reasoning at specific execution points, while loop invariant analysis, conditional (if) statement path analysis, variable initialization and mutation tracking, understanding always/never/sometimes assertion logic
Practice on CodeStepByStep
2. Earlier Date
| Difficulty: Beginner |
Time: ~10 min |
Type: function |
Write a function earlier_date that takes as parameters two month/day combinations and that prints out the earlier date. The function will take four integers as parameters that represent the two month/day combinations (month1, day1, month2, day2). Compare months first, then days to break ties. Print the earlier date in month/day format.
| Concepts: compound boolean expressions (&&, |
|
), if/else decision making, comparing multi-field data (month then day), function definition with parameters, printf formatting with %d |
Practice on CodeStepByStep
3. Logical Expressions
| Difficulty: Beginner |
Time: ~8 min |
Type: expression evaluation |
Trace the evaluation of the following expressions, and give their resulting values. Write true if the expression would have a true result and false otherwise.
int x = 42;
int y = 17;
int z = 25;
| Concepts: logical AND (&&) and OR ( |
|
) operators, logical NOT (!) operator, operator precedence with logical operators, modulo operator in boolean context, compound boolean expression evaluation |
Practice on CodeStepByStep
4. Percentage Grade
| Difficulty: Beginner |
Time: ~8 min |
Type: bare code |
| What’s wrong with the following code? Modify it to produce the intended output. Make sure to properly utilize if/else/if statements to avoid redundancy and avoid unnecessary tests. Do not use && or |
|
in your solution. |
Concepts: if vs else-if chains (mutually exclusive conditions), debugging existing code, understanding why separate if statements cause multiple matches, proper use of else for default/catch-all case, scanf and printf for interactive I/O
Practice on CodeStepByStep