Back

Activity 1.15: Combining Conditions

divider

Activity 1.15

Combining Conditions

Key Concepts

Many Facts, One Answer

AND, OR, NOT

Precedence

A Chain Asks One Question at a Time

Activity 1.13 gave you a way to pick between many outcomes — but every condition in that chain looked at one thing.

“Old enough and has a permit.” “Any one of three tanks running low.” Some questions need more than one fact before they have an answer.

OR Is True When Any Part Is

int oxygen = 88;
int coolant = 18;
int power = 132;
bool anyLow = oxygen < 25 || coolant < 25 || power < 25;
Console.WriteLine($"Any system low: {anyLow}");

Only the coolant is low. Predict what prints.

OR Is True When Any Part Is

Terminal window
Any system low: True

One out of three is enough. || asks “is at least one of these true?”

Three comparisons, three booleans, and one answer stored in one variable — which is the whole reason these operators exist.

AND Needs Every Part

bool allFine = oxygen >= 25 && coolant >= 25 && power >= 25;
Console.WriteLine($"All systems fine: {allFine}");

AND Needs Every Part

Terminal window
All systems fine: False

The coolant is 18, so one part is false, so the whole thing is false. && asks “are all of these true?”

NOT Flips It

bool isWeekend = false;
if (!isWeekend)
{
Console.WriteLine("Go to school.");
}

NOT Flips It

Terminal window
Go to school.

! takes one boolean and gives you the opposite. Read !isWeekend out loud as “not weekend.”

This is why Activity 1.11 told you to name booleans as questions. !isWeekend reads like English. !w does not.

Straight Into an if

int age = 16;
bool hasPermit = true;
if (age >= 16 && hasPermit)
{
Console.WriteLine("May drive.");
}
else
{
Console.WriteLine("May not drive.");
}

Straight Into an if

Terminal window
May drive.

A combined condition is still just a condition. Anywhere an if took one comparison, it takes this too.

Note hasPermit sits there on its own, with no == true after it. Activity 1.12 said why.

OR Does Not Spread

int day = 3;
if (day == 6 || 7)
{
Console.WriteLine("Weekend!");
}

Meant as “day is 6 or 7.” Read it the way C# will.

OR Does Not Spread

Terminal window
1-15-systems.cs(3,5): error CS0019: Operator '||' cannot be applied to operands of type 'bool' and 'int'
The build failed. Fix the build errors and run again.

|| joins two booleans. On the left is day == 6, which is one. On the right is 7, which is a number.

You have to say it twice: day == 6 || day == 7. In some languages this compiles and is quietly always true — C# will not build it.

CS0019 again, the same error as Activity 1.11's text-versus-number comparison.

AND Binds Tighter Than OR

int oxygen = 18;
int coolant = 88;
int power = 132;
bool a = oxygen < 25 || coolant < 25 && power < 25;
bool b = (oxygen < 25 || coolant < 25) && power < 25;
Console.WriteLine($"a is {a}");
Console.WriteLine($"b is {b}");

Same three numbers. Same three comparisons. Only the parentheses differ.

AND Binds Tighter Than OR

Terminal window
a is True
b is False

Without parentheses, && goes first — so a really reads oxygen < 25 || (coolant < 25 && power < 25).

You have met this before. Activity 1.6: * binds tighter than +, so 2 + 6 / 2 is 5 and not 4. Same rule, different operators.

Write the parentheses. They cost nothing and they say what you meant.

Today's Objectives

  • Combining comparisons with &&, ||, and !
  • Storing a combined answer in one named boolean
  • Writing each side of an || out in full
  • Using parentheses so precedence never decides for you

Key Terms

Logical operator
&&, ||, or ! — combines booleans into one boolean.
Compound condition
A condition built from more than one comparison.
Precedence
Which operator is applied first when you do not say.
Negation
Flipping a boolean to its opposite, written !.

'F' → Fullscreen

divider

Build

Everything today goes in a new program named 1-15-systems.

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: Three Readings, Two Answers

  • Write the code below and run it.
  • Only the coolant is low, and that is enough to make anyLow true and allFine false. Make sure you can say why for both.
  • Now set all three readings above 25 and run it again. Write down all three printed values.
  • Set the coolant back to 18 before you continue.
1-15-systems
int oxygen = 88;
int coolant = 18;
int power = 132;
bool anyLow = oxygen < 25 || coolant < 25 || power < 25;
bool allFine = oxygen >= 25 && coolant >= 25 && power >= 25;
Console.WriteLine($"Any system low: {anyLow}");
Console.WriteLine($"All systems fine: {allFine}");
Console.WriteLine($"Not all fine: {!allFine}");

Task 2: Put Them to Work

  • Add two decisions. The first uses a boolean you already stored; the second combines two comparisons on the spot.
  • Both are valid. Storing the answer first is better when you need it more than once or the name explains something; combining inline is fine for a one-off.
  • Find readings that give “STATUS: nominal” and “Short trips only” at the same time. Write them down. It is possible, and working out how is the task.
1-15-systems
int oxygen = 88;
int coolant = 18;
int power = 132;
bool anyLow = oxygen < 25 || coolant < 25 || power < 25;
bool allFine = oxygen >= 25 && coolant >= 25 && power >= 25;
Console.WriteLine($"Any system low: {anyLow}");
Console.WriteLine($"All systems fine: {allFine}");
Console.WriteLine($"Not all fine: {!allFine}");
Console.WriteLine();
if (anyLow)
{
Console.WriteLine("STATUS: attention required");
}
else
{
Console.WriteLine("STATUS: nominal");
}
if (oxygen >= 25 && power >= 130)
{
Console.WriteLine("Cleared for long haul.");
}
else
{
Console.WriteLine("Short trips only.");
}

Task 3: Plan, Then Decide on Two Inputs

  • The four comments describe a launch check. Write the code under them, leaving the comments where they are.
  • Store the two answers in named booleans first, then combine them. The names are the point — a reader should not have to work out what crewSize >= 2 was for.
  • Test all four combinations: both good, crew bad, mass bad, both bad. Only one of the four should approve.
  • Then try exactly 2 crew and exactly 499 mass, and say whether your boundaries are the ones you meant.
1-15-systems
int oxygen = 88;
int coolant = 18;
int power = 132;
bool anyLow = oxygen < 25 || coolant < 25 || power < 25;
bool allFine = oxygen >= 25 && coolant >= 25 && power >= 25;
Console.WriteLine($"Any system low: {anyLow}");
Console.WriteLine($"All systems fine: {allFine}");
Console.WriteLine($"Not all fine: {!allFine}");
Console.WriteLine();
if (anyLow)
{
Console.WriteLine("STATUS: attention required");
}
else
{
Console.WriteLine("STATUS: nominal");
}
if (oxygen >= 25 && power >= 130)
{
Console.WriteLine("Cleared for long haul.");
}
else
{
Console.WriteLine("Short trips only.");
}
Console.WriteLine();
// Ask for a crew size and convert it
// Ask for a cargo mass and convert it
// Store whether the crew is at least 2, and whether the mass is under 500
// A launch is allowed only if both are true -- approve it, or deny it

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. In your launch check, replace massOk with cargoMass < 500 || 2. Try to run it.
  2. Put it back. Now change anyLow to read oxygen < 25 || coolant < 25 && power < 25 — no parentheses — and run it with oxygen 18, coolant 88, power 132.

For each one, record the error code if there was one, and the line it names.

One of these will not build. The other builds, runs, and gives a different answer than the version with parentheses. Say which is which, and what the second one printed instead.


Challenge (Optional): Say the Same Thing Backwards

  • allFine and anyLow look like opposites. Add a line that stores !anyLow and print it next to allFine.
  • Try several sets of readings, including ones where two systems are low. Do the two values ever disagree?
  • Write a comment saying whether !anyLow and allFine mean the same thing, and how you convinced yourself.
divider

Same Idea, Exam Notation

C#
bool canLaunch = fuelOk && crewOk;
bool anyFault = fuelLow || powerLow;
bool allClear = !anyFault;
Exam Notation
canLaunch ← fuelOk AND crewOk
anyFault ← fuelLow OR powerLow
allClear ← NOT anyFault

This is one of the easy panels. The exam writes the operators as words, and the words are exactly what the symbols mean.

MeaningC#Exam notation
Both must be true&&AND
At least one must be true||OR
Flip it!NOT

Reading them aloud is a genuine exam technique. NOT anyFault says what it does. !anyFault says the same thing, and the exclamation mark is easy to miss in a long line — which is exactly how it gets misread under time pressure.

The precedence rule is the same in both. The reference sheet applies NOT first, then AND, then OR — which is C#'s order exactly. An exam question that omits the parentheses is testing whether you know that.

divider

Checkpoint

Your finished program should run like this.

Example Output
Any system low: True
All systems fine: False
Not all fine: True
STATUS: attention required
Cleared for long haul.
Crew size? 1 [Enter]
Cargo mass? 300 [Enter]
LAUNCH DENIED
  • Every boolean prints True or False, with a capital letter
  • You have the three printed values from Task 1 with all readings above 25
  • You found readings that give nominal and short trips only together, and wrote them down
  • All four Task 3 combinations were tested, and exactly one approves
  • Both Task 4 results are recorded — which one refused to build, and what the other one printed instead
divider

Reflection

Answer the following questions before submitting your work.

  1. day == 6 || 7 reads like correct English but will not compile. Explain what || actually needs on each side.
  2. Task 4's second change built and ran without a single warning, and gave a different answer. Whose job is it to catch that kind of mistake, and what is the cheapest way to avoid it?
  3. You could write anyLow as one combined condition, or as three separate if statements. What does storing it in a named boolean give you that neither of those does?
divider

Submit

Submit four things to the dropbox:

  1. Your 1-15-systems program file.
  2. Your Task 1 all-above-25 values, and the readings that gave nominal and short trips only together.
  3. Your four Task 3 combinations, and both Task 4 results with error codes and line numbers.
  4. Your three reflection answers.

Activity Complete