Working with Boolean logic

Computer programs are made up data structures which use conditions and decisions that bring the desired outputs. We will use Python notation here, as it is simple, and you may have seen it before. The basic data structures are:

  • Iterators such as while loops and for loops. An iterator loops as many times as it is told to, running other commands each time it goes around
  • Decision Points such as If structures and Case structures. The preceding image is a diagram of a set of nested If structures

    Boolean Operators

     

    Notation

    Description

    X == Y

    X is equivalent to Y. This is not always a numeric value set

    X != Y

    X is not equivalent to Y

    X <= Y

    X is smaller than OR equivalent of Y

    X >= Y

    X is greater than or equivalent of Y

    X < Y

    X is less than Y

    X > Y

    X is greater than Y

  • X and Y are both true
  • X and Y are both false
  • Either X or Y is true
  • Anything but X
  • Anything but Y

    Boolean Variables

     

    Variable

    Description

    AND

    Produces a Boolean comparison that is only true if all the elements are true.

    OR

    Produces a Boolean comparison that is true if any of the elements are true.

    NOT

    Produces a Boolean comparison that is only true if all the elements are not true.

The following image is testing the two conditions of X against a Boolean variable of NOT. You are probably starting to see how outputs can be drawn from many different internal coding choices. The attacker or original could be testing a condition by any of a number of conditions, so you have to think of all the ways that the output might be obtained.

Working with Boolean logic

Reviewing a while loop structure

A while loop is explicitly started and stopped by true/false choicepoints. These can look very complicated, but they resolve to a limited set of tests for a single condition.

X = 0
Y = 20
while (X != Y): print (X), X = X + 1

This Python 3 loop will print the value of X over and over until it reaches 10, then stop. It would work exactly the same if we said while X < Y, because the loop structure is testing X as it is incremented. A more complicated loop using a random number for the incrementer element might go on for much longer (or not) before it randomly hits on a value of X that was the equivalent of Y.

Reviewing a while loop structure

It is obvious that the program is testing the looping condition each time. Here is an example using that random X value. First the X value is chosen, then the print (X) command is run twice. Since X was only set once in the first line, it didn't change in the two print commands. When the value of X was reset, it printed a different value. The condition was that X would not equal Y. We set the value of Y a few lines up, so it does not need to be reset to run this example. The reason why X returned only once was that the second time through, X was randomly set to 11. The odds of it being set to 11 from the random draw was 1 out of 11, a far better chance than your probability of winning the PowerBall Lottery.

Reviewing a while loop structure

If we run the loop again, it might run more times, as it randomly avoids a value of X equivalent to Y. Again, it does not print the value of X = 11, because that is precluded by the while loop condition.

Reviewing a while loop structure

Reviewing the for loop structure

A for loop doesn't need an incrementer because it builds the range into the condition, as contrast to a while loop that only includes a limit beyond which the loop will not run. Using Python notation, the following image shows what happens if you start with an X value of 0 and a range from 1 to 11. The preset value of X is not important to the while loop iteration. It applies all values to X that it tests.

Reviewing the for loop structure

We are starting with X set to 100, but the for loop takes the X value from its own condition.

Reviewing the for loop structure

If you really want X to remain a constant, you can use it as the base of a different range, as shown in the following image.

Reviewing the for loop structure

Understanding the decision points

An If structure is a binary decision: either yes or no. A light switch on the wall is a physical example of an if structure. If the switch is in one position, the lights are on, and if it is in the other position, the lights are off:

Understanding the decision points

A Case Structure is a decision structure with more than one "right answer", more than one "yes", and not a single "no". An example of this might be an ice cream dispenser with three flavors, chocolate, strawberry and vanilla. If you do not want ice cream, you do not even approach the machine. You have three choices and they are all correct:

Understanding the decision points
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47