Back

Activity 1.16: Nested Conditions and Randomness

divider

Activity 1.16

Nested Conditions and Randomness

Key Concepts

A Decision Inside a Decision

Nest, or Use AND

Random Numbers

DENIED Does Not Say What to Fix

if (crewSize >= 2 && cargoMass < 500)
{
Console.WriteLine("LAUNCH APPROVED");
}
else
{
Console.WriteLine("LAUNCH DENIED");
}

This is where Activity 1.15 left the launch check, and it is correct. With a crew of 1 and a mass of 300, predict what the crew is told.

DENIED Does Not Say What to Fix

Terminal window
LAUNCH DENIED

Two rules were checked and the crew learns only that one of them failed. The && threw away which one. That is what it is for — it turns several facts into one answer, and one answer cannot name a reason.

A Decision Inside a Decision

if (crewSize >= 2 && cargoMass < 500)
{
Console.WriteLine("LAUNCH APPROVED");
}
else
{
if (crewSize < 2)
{
Console.WriteLine("DENIED: crew too small");
}
else
{
Console.WriteLine("DENIED: cargo too heavy");
}
}

A Decision Inside a Decision

Terminal window
DENIED: crew too small

The inner if sits in the outer one's else block, so it is only ever asked once the launch has already been denied. By then the question is no longer whether something is wrong — it is which thing.

An Inner Condition Is Only Asked If the Outer One Led There

  • Indentation shows the depth. Each level in is four more spaces, the same as every block you have written.
  • Every opening brace needs its partner, and at the bottom of a nest they stack up several deep.
  • Read a nest from the outside in. The inner question already knows everything the outer one decided.

If your braces stop matching, your indentation was lying to you before the compiler noticed.

Nest, or Use AND?

  • Use && when you only care whether everything passed — one answer, one message.
  • Nest when you have to respond differently to each reason it failed.

Both are correct code. They answer different questions, and which one you want depends entirely on what the program has to say next. A launch check that prints one word does not need a nest; one that tells the crew what to unload does.

Randomness Arrives in One Call

int roll = Random.Shared.Next(1, 7);
Console.WriteLine($"You rolled: {roll}");

Every program you have written so far gave the same answer every time you ran it. This one will not.

Randomness Arrives in One Call

Terminal window
You rolled: 4

Run it again and you get something else. Activity 1.6 promised you this one. The exam sheet writes it RANDOM(1, 6), and back then the honest answer was that C# needed more than you had. It does not need much.

The Second Number Is Not the Number You Want

RANDOM(1, 6) on the exam sheet includes both ends — it can give a 1 and it can give a 6.

Random.Shared.Next(1, 7) is how you say that in C#. The first number is included. The second one is where it stops, and stopping at 7 is how you get a 6.

The Second Number Is Not the Number You Want

int roll = Random.Shared.Next(1, 6);

So this is the mistake everyone makes once. It is a die that looks right, compiles clean, and runs forever. Predict what is wrong with it.

A Die That Never Rolls a Six

60,000 rolls
1: 11794
2: 12071
3: 12154
4: 12101
5: 11880
6: 0

Sixty thousand rolls and not one 6. Not rare — impossible. And nothing anywhere said so: no error, no warning, and five of the six faces behaving perfectly.

You could not have caught this by running it. Roll it ten times and getting no 6 is ordinary luck. That is what makes this different from every bug so far.

Backwards It Does Catch

int roll = Random.Shared.Next(7, 1);

Same two numbers, the wrong way round. Predict whether this one is silent too.

Backwards It Does Catch

Terminal window
Unhandled exception. System.ArgumentOutOfRangeException: 'minValue' cannot be greater than maxValue. (Parameter 'minValue')

It stops the program on the spot. The obvious mistake is caught and the subtle one is not — which is usually the way round it goes, and the reason testing is a skill rather than a formality.

Today's Objectives

  • Placing a conditional inside another conditional
  • Choosing between nesting and &&
  • Generating a random whole number in a range
  • Testing a program whose output changes every run

Key Terms

Nested conditional
A conditional written inside another conditional's block.
Nesting depth
How many levels deep a piece of code sits.
Exclusive upper bound
A range that stops before its second number rather than including it.
Deterministic
Giving the same result every run. Everything before today was.

'F' → Fullscreen

divider

Build

Everything today goes in a new program named 1-16-nested.

How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.

The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.

Where a block shows only comments, you write that code yourself. The comments stay in your file.


Task 1: Say Which One Failed

  • Write the code below and run it. This is Activity 1.15's launch check with a decision added inside the else.
  • Produce all three messages by editing the two numbers: an approval, a crew failure, and a cargo failure.
  • Then try crew 1 and mass 900, where both rules are broken. Write down what it reports, and whether you think that is acceptable.
1-16-nested
int crewSize = 1;
int cargoMass = 300;
if (crewSize >= 2 && cargoMass < 500)
{
Console.WriteLine("LAUNCH APPROVED");
}
else
{
if (crewSize < 2)
{
Console.WriteLine("DENIED: crew too small");
}
else
{
Console.WriteLine("DENIED: cargo too heavy");
}
}

Task 2: Add a Die Roll

  • Set the crew and mass back to 1 and 300, then add the roll and the decision that reads it.
  • Run it five or six times and write down what you rolled each time. Nothing you have written before today could do this.
  • Random.Shared.Next(1, 7) gives a whole number from 1 to 6. The 7 is where it stops, not a face on the die.
1-16-nested
int crewSize = 1;
int cargoMass = 300;
if (crewSize >= 2 && cargoMass < 500)
{
Console.WriteLine("LAUNCH APPROVED");
}
else
{
if (crewSize < 2)
{
Console.WriteLine("DENIED: crew too small");
}
else
{
Console.WriteLine("DENIED: cargo too heavy");
}
}
Console.WriteLine();
int roll = Random.Shared.Next(1, 7);
Console.WriteLine($"Solar flare check, rolled: {roll}");
if (roll >= 5)
{
Console.WriteLine("Flare detected.");
}
else
{
Console.WriteLine("Skies clear.");
}

Task 3: Plan, Then Roll Two Dice

  • The comments describe a doubles check with another decision inside it. Write the code under them, leaving the comments where they are.
  • The indentation in the comments is telling you the shape. Two of those lines are pushed further right because they belong inside the match check, not beside it.
  • Run it until you see a match. It takes about six tries on average, which is worth noticing on its own.
1-16-nested
int crewSize = 1;
int cargoMass = 300;
if (crewSize >= 2 && cargoMass < 500)
{
Console.WriteLine("LAUNCH APPROVED");
}
else
{
if (crewSize < 2)
{
Console.WriteLine("DENIED: crew too small");
}
else
{
Console.WriteLine("DENIED: cargo too heavy");
}
}
Console.WriteLine();
int roll = Random.Shared.Next(1, 7);
Console.WriteLine($"Solar flare check, rolled: {roll}");
if (roll >= 5)
{
Console.WriteLine("Flare detected.");
}
else
{
Console.WriteLine("Skies clear.");
}
Console.WriteLine();
// Roll a second die, 1 to 6, into its own variable
// Show the second die
// If the two dice match, it is a critical event
// inside that, if the matched value is 6, say "CRITICAL: total systems failure"
// otherwise say "CRITICAL: minor fault" and show the value
// If they do not match, say "No event."

Task 4: Break It on Purpose

Two changes, one at a time. Write down exactly what happens, and put each back before the next.

  1. Change your first roll to Random.Shared.Next(1, 6). Run the program twenty times and tally every value you get.
  2. Put it back. Now change it to Random.Shared.Next(7, 1) — the same two numbers, the wrong way round — and run it once.

For number 1, record your twenty values and the highest one you saw. For number 2, record the error and the line it names.

One of these stopped the program immediately. The other ran perfectly twenty times in a row and is still wrong. Say which is which, and how many more runs you would want before you would be willing to call the first one broken.


Challenge (Optional): Switch the Randomness Off

  • A program that behaves differently every run is hard to test. Near the top, replace the random roll with int roll = 6; so you can choose the value yourself.
  • Use it to reach your total systems failure branch without waiting for double sixes. You will need to force the second die too.
  • Write a comment explaining why a programmer might turn randomness off on purpose, then put the random version back before you submit.
divider

Checkpoint

Your finished program should run like this — though yours will not match, and should not. The two dice are different every run, and this particular one is a double six.

Example Output
DENIED: crew too small
Solar flare check, rolled: 6
Flare detected.
Second die: 6
CRITICAL: total systems failure
  • All three Task 1 messages were produced, and you recorded what crew 1 with mass 900 reports
  • Your six Task 2 rolls are written down, and they are not all the same number
  • Task 3's doubles check is nested inside the match check, not beside it
  • You ran the program until you saw a match at least once
  • Both Task 4 results are recorded — twenty values and a highest for one, an error and a line number for the other
divider

Reflection

Answer the following questions before submitting your work.

  1. Task 1 reports only one reason even when both rules are broken. Explain why the nest behaves that way, and describe a situation where reporting only the first problem would be the wrong choice.
  2. Random.Shared.Next(1, 6) never produces a 6, and nothing warns you. Explain why running the program a few times is not enough to catch that, and what you would do instead.
  3. You could write Task 1's check with && or with a nest. Describe what each one is better at, and what the program has to do for the difference to matter.
divider

Submit

Submit four things to the dropbox:

  1. Your 1-16-nested program file.
  2. Your Task 1 answer for crew 1 with mass 900, and your six Task 2 rolls.
  3. Your Task 4 tally of twenty values with its highest, and the error and line number from the second change.
  4. Your three reflection answers.

Activity Complete