Clio Corvid

Writer – Teacher

Menu
  • Welcome
  • Writing
    • Poetry
    • Fragments
    • AI-generated
  • Mathematics
    • Algebra
    • Calculus
    • General
    • Geometry
    • Notation
    • Pedagogy
    • Puzzles and Memes
  • Reflections
    • Diary
    • Reflections
    • Bein’ Enby (Medium)
    • Inside My Mind
    • Other essays
  • Closed Blogs
    • Cerebri Laevi
    • Father’s Opinion
    • Good Men Project
    • Into the Labyrinth
    • Sisyphus Winced
    • Prawn Salad, Ltd.
Menu

It’s the little things…

Posted on June 10, 2010July 27, 2021 by Clio

Doing some code clean-up on Battleships this morning, I was reminded of a detail of C# logic precedence that I’ve used to my benefit elsewhere (including within the program), but which caused a brief hiccup in one instance. Specifically, while the logical and and or operators are transitive, that’s not strictly true of && and ||.

In this case, I had broken my large and increasingly complex ResetGrid method into several smaller ones. I have a boolean variable, blnAllDone, which indicates whether the current puzzle has been solved. I created the subroutines which count the remaining cells and the remaining ships to return a boolean value, which would be true if the remaining cells or ships is zero,  false otherwise. This was the original code:

bool blnAllDone = CalcRemainVals();
blnAllDone = blnAllDone && CalcRemainShips();

However, the CalcRemainShips method only ran if the puzzle was complete. As soon as I looked again at the code, I realized why: C# stops assessing x && y if x is false (since x && y can’t be true if x is false), and returns false. Likewise, c# stops assessing x || y if x is true, and returns true. This is a useful feature if you want to shortcut situations where a variable might not be set, for instance, but in this case it led to unexpected results.

I made a minor change, and all was well with the world again:

bool blnAllDone = CalcRemainVals();
blnAllDone = CalcRemainShips() && blnAllDone;

Share this:

  • Email a link to a friend (Opens in new window) Email
  • Print (Opens in new window) Print
  • Share on Facebook (Opens in new window) Facebook
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Mastodon (Opens in new window) Mastodon
  • Share on X (Opens in new window) X
  • Share on WhatsApp (Opens in new window) WhatsApp

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Checking In
  • Fractious Fractions
  • Into the Cornfield
  • How Soon Is Now?
  • Roman Re-enacting: Malden 2025

Archives

Log in
©2026 Clio Corvid