JumpStart Live Day 4: Boolean Zen and Advanced Loops

JSL Day 4 covers the following:

  • Boolean zen
  • Advanced loops

Boolean Zen

The code we write is just as important as the way we write it.

Boolean zen describes code using boolean values written in the most concise way possible.

If you are comparing to true or false in an if-statement, while loop or until loop, that’s not boolean zen.

In other words, ==true or ==false false is no good!

Why?  We don’t need it!  Things will automatically evaluate as true or false without the comparison.

Here are some examples:

example 1
Example 1

We don’t need to write if-statements that print true or false.

Instead, we can just puts the boolean statement itself…since it automatically evaluates as true or false on its own!

The if-else structure is wholly unnecessary.

example 2
Example 2

We don’t need to compare correct to true.  That’s because correct by itself will either evaluate to true or false.

example 3
Example 3

This is similar to Example 2, except it uses a while loop.

We still don’t need to compare not_correct to true!  That’s because the variable not_correct alone already holds a value of true or false.

This will come up more when I start writing functions.

Advanced Loops

This section explains a little bit about:

  • Fencepost problems
  • Loops with logical operators

Fencepost Problems

Imagine building fences with posts and wire.

In fact, let’s draw a picture:  |-|-|-|-| with the | representing a fence post and the - representing the wire of the fence

You’ll need one more post than you do wire sections.

Loops follow a similar structure.  However, when writing loops, it’s easy to have a bit of unwanted “wire” just hanging off the end.

In programming, this is also known as a “loop-and-a-half”.

A common solution?  Make the loop run one fewer times than needed — and handle that last “post” outside of the loop itself.

Here’s an example:

example 1
Fencepost problem: Example 1

If the user enters 3 as max, we’d want the loop to print 1, 2, 3 — three numbers, but only two commas.

 

This solution prints the first “fencepost” outside of the loop (with print 1 ).

Inside the loop, we print commas…followed by the next number.

Another example:

example 2
Fencepost problem:  Example 2

You can also do the loop first…and then print the last “fencepost” outside the loop.

Let’s say the user enters 3 as max again.

The loop goes up from 1 to max - 1.  That means it will print 1, 2, …and then the loop ends.

But outside the loop, we’ve got print max.  That prints 3…and the fence is complete!

Loops with Logical Operators

Let’s take a closer look at using logical operators with loops — specifically, while loops and until loops.

An example:

example 1

 

While the number is:

  • less than 1  (not greater than 0)
  • OR greater than 99 (not less than 100)…

    We’ll prompt the user to re-enter their number.

We cannot put AND — because it’s not possible for a number to be less than 1 AND greater than 99.

Another example:
example 2

 

Until the number is:

  • Greater than 0
  • AND less than 100…

We’ll prompt the user to re-enter their number.

We cannot put OR — because you could enter 101, and 101 is greater than 0.  That means 101 would stop the loop, even though it doesn’t meet both requirements.

Summary

This lesson talks about boolean zen and advanced loops!

Boolean zen is another way of saying “writing clean, concise boolean statements”.

Here’s a good rule of thumb:  Never compare a variable to true or false.

Why?  Because the variables will automatically evaluate to true or false on their own.

When working with advanced loops, we’ll encounter fencepost problems (aka loop-and-a-half).

To avoid unwanted “wire”, we can make the loop run one less round than needed…and insert an additional “fencepost” outside of the loop.  We can insert these extra “fenceposts” either before or after the loop in question.

Lastly, we must pay attention to using logical operators inside loops — namely, until and while loops.

Here’s why:  Because similar problems will require different operators, depending on how you frame the condition for breaking the loop.

…And that’s all for now!  Stay tuned for the Day 4 Exercise.  🙂

Leave a comment