Real-World Decision Scenarios, Explained with Code Examples

UPI Study › Courses › C++ Programming › Free lessons › Lesson 4.7 Free with a free account

Real-World Decision Scenarios

C++ Programming · Chapter 4: Decision MakingSection 4.7 · lesson 27 of 84video coming soon

← Deeply Nested ConditionsNext lesson: The Rationale Behind Loops →

Earn 3 college credits for C++ ProgrammingRecommended for credit by NCCRS · Computer Science 242 · self-paced · one payment, lifetime access

In this lesson

From a rule somebody said to code you can defend.

  • Turn a written rule into a decision table, then a chain
  • Order range tests so each needs one comparison
  • Tell banding from slabbing
  • Guard against impossible input first

Transcript

Everything said in the video, section by section (1,346 words). Read along, or search it.

Lesson Overview

From Written Rule to Tested Chain

Nothing new is introduced in this lesson. Every construct here has already been met. What hasn't been practiced is the part that actually causes trouble: taking a rule written in business language, pinning down what it really says, and handling the cases nobody mentioned. Every scenario follows the same three steps. First, the written rule, as somebody said it. Ask what happens exactly at each boundary, and what happens if two parts apply. Second, the decision table, with every case written once. Third, the chain, with one branch per outcome, tested with one run per table line, plus each boundary value. Written rules are ambiguous. Tables aren't. The table costs a few minutes, and removes almost every boundary fault before a line is written.

Scenario One

Marks to Letter Bands by Order

Scenario one: marks to a letter band. Ninety and above is an A, the eighties a B, seventies a C, sixties a D, and below sixty an F. The chain tests at least 90, then at least 80, then 70, then 60, and otherwise F. The most useful idea is that the order carries half the logic. Reaching the second test already guarantees the mark is below 90. So at least 80 means in the eighties, without anyone writing it. Each band needs just one comparison. A 95 gets an A at the first test.

Quick Check

Reversing the Band Order

Now reverse the order. Every test still compiles. Quick check: what does a 95 come out as? Pause, and trace it.

Answer

A 95 Becomes a D

A D. The first test it meets, at least 60, is true, and the chain stops there. The order was half the logic.

Test Values

Boundaries and Impossible Marks

Which values are worth trying? Not 75 and 85. Those confirm what you already believe. Try 90, the first score that earns an A. Then 89, one mark short of it. Then 60 and 59, either side of the lowest passing band. Every fault in a banded rule lives at a boundary, or outside the range altogether. Then 101. Without a guard, this chain quietly reports an A. And minus 1 quietly reports an F. The sentence never said what to do with them. They can't occur, but they'll arrive eventually anyway.

Scenario Two

Tax Slabs Charge Only Their Portion

Scenario two: tax slabs. Nothing up to 250,000. Five percent from there to 500,000. Twenty percent to a million. Thirty percent above that. This rule looks like the last one, and behaves entirely differently. The bands don't select a rate for the whole amount. Each rate applies only to the portion of income inside that slab. Take an income of 700,000. The first 250,000 is taxed at nothing. The next 250,000 at five percent: 12,500. And the next 200,000 at twenty percent: 40,000. The top slab is never reached. The total is 52,500.

Code Walkthrough

Descending Chain With Slab Constants

Here's the code, again as a descending chain. Above a million, 112,500 plus 30 percent of the excess over a million. Above 500,000, 12,500 plus 20 percent of the excess. Above 250,000, five percent of the excess. Those constants aren't magic. 12,500 is the second slab charged in full. 112,500 is that, plus the third slab in full. Run 700,000, and it gives 52,500. Charging 20 percent on the whole amount gives 140,000: nearly three times too much. So ask one question. Does the rate apply to the whole value, or only to the part inside the band? Confusing them makes answers wrong by a factor.

Scenario Three

Discount With Two Conditions

Scenario three: a discount with two conditions. Members get 15 percent on orders of 3,000 or more. If only one of those is true, 10 percent. Otherwise nothing. Two conditions give four combinations. Member with a large order: 15 percent. Member with a small order, or non-member with a large order: 10. Neither: nothing. Writing it out settles a question the sentence leaves open. A non-member with a big order really does get 10 percent. Those two middle cases collapse into one branch, joined with OR, because they take the same action.

Quick Check

Swapping the Discount Tests

The order matters, too. Here the two tests are swapped. Quick check: what does a member with a large order get now? Pause, and trace it.

Answer

Fifteen Percent Goes to Nobody

Ten percent. The OR test is also true for the first case, so it catches it, and the 15 percent branch can never run. Fifteen percent goes to nobody at all. The most specific case always goes at the top.

Scenario Four

Leap Years and Exceptions to Exceptions

Scenario four: leap years. Every fourth year is a leap year, except years divisible by 100, which aren't, unless they're also divisible by 400, which are. It's the classic rule whose exceptions have exceptions. And the simple version, divisible by 4 alone, is wrong only 3 years in every 400: rarely enough to survive casual testing. Four years exercise every route. 2023 fails the first test. 2024 is the ordinary case: a leap year. 1900 is divisible by 4, but the century exception applies, so it isn't. And 2000 is divisible by 400, the exception to the exception, so it is. As one expression: divisible by 4 and not by 100, or divisible by 400. The same rule can be written as a chain, reading in the same order as the sentence, most specific exception first. Divisible by 400: true. Otherwise, divisible by 100: false. Otherwise, divisible by 4: true. Otherwise false. Run the two tricky years, 1900 and 2000, and both answers match. The expression is shorter. The chain is easier to check against the sentence. Use whichever you can defend.

Scenario Five

Menu Routing Needs a Final Else

Scenario five: routing a menu choice. Four options: one converts, two reports, three clears, four quits. This is the simplest scenario, and it's included for one reason: the branch that handles everything else. A person will eventually type 7, or 0, or a letter. With the final else, the program says: that is not one of the options. Take it away, and the program does nothing at all, silently. That reads as a broken program. Nobody stated that line in the rule, and every menu ever written needs it.

Scenario Six

Guarding Against Below Absolute Zero

Scenario six: convert Celsius to Fahrenheit, but refuse anything below absolute zero. This is the shape most real tasks take: check first that the work is worth doing, and only then do it. A test written to reject rather than select is called a guard. At the top, it keeps the real work at one level of indentation. The conversion uses 9.0 over 5.0, not 9 over 5. With whole numbers the division gives 1, and 100 degrees comes back as 132. And the boundary is minus 273.15 itself. The rule says below, so exactly that value is accepted. Decide that deliberately, and test it. Here is that mistake for real: 9 over 5 in whole numbers. A hundred degrees comes back wrong by a consistent, very confusing amount.

Practice Set

Five Rules to Table and Test

Now a practice set. For each rule, produce the decision table, the chain, and the values you'd try, including every boundary and one impossible input. Parcels cost 20 up to half a kilogram, 35 up to 2, and 60 beyond. Pin down exactly what 0.5 and 2.0 cost, and whether zero weight is possible. Under 5 or over 65 travel free. Does over 65 include the birthday? Electricity at 3 a unit for the first 100, 5 for the next 200, 8 after. Banding or slabbing? Reread scenario two. Priority if urgent, or fragile and outside the city: two readings exist. And a pass at average 40, provided no mark is below 25. A solution is finished when every table line has run, every boundary has been tried from both sides, and an impossible input was handled. Otherwise it's demonstrated, not tested.

Key Takeaways

Rule, Table, Chain, Boundary Tests

To summarize. Work from the written rule, to a decision table, to a chain, then test one run per row, plus every boundary. Order range tests from one end so each needs one comparison, and put the most specific case first. Slab rules charge only the part of a value inside each band. And guard against impossible input first, and always handle anything else. Here's something to think about. In the tax code, which branch handles an income of exactly 500,000, and what does it charge? Does that match the slab table?

About C++ Programming at UPI Study

C++ Programming (Computer Science 242) is a self-paced online course of 84 video lessons in 11 chapters, from your first program to decisions, loops, arrays, functions, classes, files, pointers and inheritance. It carries a NCCRS credit recommendation for 3 semester credits.

Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Why take it here

Why students take C++ Programming with UPI Study

Your first real programming languageC++ teaches how programs really work: variables, control flow, functions, memory and objects. It is the base for data structures, algorithms, systems and game programming, and for most computer-science degrees.
Credit at a fraction of the priceThe same 3 credits can cost $750 or more at a US college. Here it is one payment, self-paced, with lifetime access.
Finish your degree soonerTransfer credits reduce the courses you still owe, so you can graduate earlier or lighten a heavy semester.
Strengthen an applicationA completed college-level course on an official transcript shows admissions and scholarship committees you can do the work.
Study on your own scheduleNo start dates, no deadlines. Watch the lessons, practice, and take the final when you are ready.
130,000+students helped with college creditsACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Where the credit goes

Universities that work with NCCRS

C++ Programming carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.

Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.

FAQ

Questions students ask us

About Real-World Decision Scenarios

How do I turn a written rule into a decision table and then an if-else chain in C++?
Start by asking what the rule says exactly at each boundary and what happens when two parts apply. Then write a decision table that lists every case once, and build a chain with one branch per outcome. Test with one run per table line plus every boundary value. The table takes a few minutes and removes most boundary faults before any code is written.
Why does the order of conditions in an if-else if chain matter?
Each test runs only if every earlier test failed, so the order carries part of the logic. Testing marks >= 90, then >= 80, then >= 70 needs just one comparison per band. Reverse the order and a 95 matches marks >= 60 first, so it gets a D. Put the most specific case first, such as the member with a large order before the OR case.
What is the difference between banding and slabbing in a decision rule?
Banding uses the band a value falls in to pick one result for the whole value, while slabbing applies each rate only to the part of the value inside its slab. With nothing to 250,000, 5 percent to 500,000, and 20 percent to a million, an income of 700,000 owes 12,500 plus 40,000, which is 52,500. Charging 20 percent on the whole amount gives 140,000.
Why should I check for impossible input before doing the real work in a decision chain?
A guard is a test that rejects bad input at the top, so the real work runs only on valid values. A grade chain with no guard quietly reports an A for 101 and an F for -1. For a Celsius converter, reject values below -273.15 first, then convert using 9.0 / 5.0, since 9 / 5 in integers gives 1. Test -273.15 itself, because it is accepted.

About the C++ Programming course

How many college credits is C++ Programming?
C++ Programming (Computer Science 242) carries a NCCRS credit recommendation for 3 semester credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
How much does C++ Programming cost and what is included?
$250, paid once, with lifetime access: all 84 video lessons, a practice quiz after each, the graded work and final exam, and an official transcript when you finish. Students who want several courses choose UPI Study PRO instead.
Which universities accept UPI Study credits?
UPI Study has signed partner agreements with universities such as Thomas Edison State University, University of the People and Charter Oak State College, and works with universities such as Penn State, SNHU and SUNY. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities. Use "Find my college" on this page to check yours.
How long does C++ Programming take?
It is self-paced with no deadlines. The 84 lessons total about 0 hours of video; most students add reading and practice time and finish in a few weeks to a few months, on their own schedule.
How are the exams done?
Quizzes and assignments are online in the course. The final exam is taken online with proctoring; a person on the UPI Study team checks assignments, usually within two days.
How do my credits reach my college?
When the course shows 100% complete, request the official transcript from your dashboard. It is sent to the college you name, and you can send it to more than one. Check your college first with "Find my college" on this page, or ask Athena.

Ready for the full course? Earn 3 credits in C++ Programming.

84 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.

C++ Programming
3 credits · NCCRS
Enroll →