PAT - More Conditionals (Lesson)
More Conditionals
First Impressions
Conditions are found all around us. We can make a condition out of all most normal things that happen. These are logic statements symbolized in math by if p then q. The "p" is the hypothesis, and the "q" is the conclusion or result. For example:
Are these the same?
Are these the same? Yes, one is in written in sentence form and one is written in pseudocode form, but both indicate the same condition and result of the condition if the recipe is to be made according to recipe directions.
What is the "else" result of the above compound if statement?
The recipe does not need cinnamon or I have cinnamon.
What is the hypotheses?
If the recipe needs cinnamon and there is no cinnamon.
What is the conclusion?
Then I will go buy cinnamon.
Now, let's look at another condition. Both A and B begin with the compound conditional with an "or " condition. Hover over each check to reveal the answers.
A conditional statement is called a decision statement because a decision, a choice, a branch must be taken based on the truth of the statement. If the condition is true, the next statement executes, otherwise the code branches to the else or the statement following the conclusion of the if else statement.
Since the if statement provides a conclusion, an answer, an if statement is a type of algorithm providing a solution to a small problem. The problem is the hypothesis, the condition. The solution is the result of the condition evaluation. If there is no else associated with the if, then if the condition is false, there is no solution within the if for the false condition. Many times, the if statement is only a small part of the overall problem that is being solved.
Variable Types
Before we go forward, let's look at the types of information that could be in a variable x or a variable with any valid name. A variable must be defined as a type so that the computer understands how to store the information that you would like. The variable name may be made up of letters, numbers, and some symbols, but may not start with a number or contain a space. Reserved symbols such as "/" a division symbol are not allowed. The variable types are in the table below.
Type of Variable |
Data Type Definition |
---|---|
decimal |
Numbers with a fractional part to the right of the decimal point. Example 24.56 or -32.0671 |
integer |
Numbers that are whole numbers or numbers without a fractional part. All the numbers ..., -2, -1, 0, 1, 2, ... |
boolean |
Has only two values, true and false, which equate to 1 or 0 respectively when stored the value is stored by the computer |
string |
Characters |
Decimal and integer variables work just as they do in math. The variables hold the values that the program assigns to them and proceeds with calculations.
Let's look at the boolean data type and how it could be used. Remember, a boolean is either true or false so is great for a conditional statement. Taking the raining example from above, the right-hand side has been rewritten using boolean variables. The left-hand pseudocode has been turned into boolean variables.
What if I rewrite the above like this:
If itIsRaining
If stayDry
getUmbrella
The variable itIsRaining has been defined as a boolean, so the value stored for itIsRaining is either a zero or a 1. If a 1 is stored, when itIsRaining is tested, the condition tests to true and then stayDry is tested for truth. If stayDry is a 1, it is true, then getUmbrella, the steps to get the umbrella is performed. This is the procedure or method that is separate. We know what we need to do and get the umbrella without thinking through all of the steps. Thus an abstract piece of code that works without having to tell everyone all the details.
If a 0 was stored in itIsRaining,itIsRaining would test as false and I would not do any of the rest of the code in the nested if statement above. A boolean always tests true or false, nothing else. The computer uses the 0 or 1 in storage because it is the shortest way to store the decision choice.
In the image, the variable itIsRaining has been defined as a boolean, so the value stored for itIsRaining is either a zero or a 1. If a 1 is stored, when itIsRaining is tested, the condition tests to true and then stayDry is tested for truth. If stayDry is a 1, it is true, then I carry the umbrella.
Note the camelCase use of the variable naming. Variables by convention should start with a lower-case letter and subsequent words should be capitalized to allow our eyes to quickly pick out each word and read it.
Boolean variables are assigned the 0 and 1 values in the program just like other variables are assigned numbers by return true for 1 and false for 0.
Lastly, the string data type consists of characters that could include spaces and symbols for example "How are you?". The quotes set the character string apart from other items and indicate that everything inside the quotes should be stored in the variable.
There are other conditional items called loops that are used in the decision-making process. These will be examined in the next lesson under Loops.
Short Circuit
A short circuit means that on a compound condition the first condition will determine whether the second condition needs to be read and evaluated. To work quickly, a computer uses this technique to move along without having to evaluate all of the code. You should to.
Remember,
"and" requires the condition on both sides of the "and" to be true
"or" requires only one condition on the two sides of the "or" to be true.
Examples: Suppose a = 5 and b = -3
if a < 6 and b < 0
the first condition is true so the second is evaluated. An "and" condition needs both sides true to be a true condition.
if a < 3 and b < 0
the first condition is false so Short Circuit, there is no need to evaluate the next as one false makes the complete "and" condition false.
if a < 3 or b < 0
the first condition is false but the second condition may be true to make the complete statement true, so evaluation of the second statement is needed. An "or" statement only needs one true.
if a < 6 or b < 0
the first condition is true so there is no need to evaluate the second condition, so Short Circuit, since the statement is already true. It does not matter whether the second condition is true or false. An "or" statement only needs one true.
Abstraction
Remember the definition of abstraction? Abstraction basically means that we as programmers do not need to know everything. We can accept that the procedure or function statement in Alice (also called methods) will work as designed by the programmers of these. If not, we would send them a "bug" report so that they could address the situation and correct the code. Quality in coding means testing well, but "bugs" do happen by unexpected circumstances or combinations of items. We will examine debugging later in this module.
Our past discussion for boolean provides a great example of abstraction.
Using the if statement, the condition of truth is the abstraction. Sometimes we can see it and other times we cannot. If itIsRaining? Do we see the code that is checking whether itIsRaining is a 0 for false or 1 for true. No, but it works! This code is abstracted and handled for us, reducing the complexity of each programmer having to write code in his or her program to check this.
Later in this module, we will learn to write code in our program that is written once, but used many times.
Abstraction is about keeping complex items simple. Write it once and use it as often as is needed. This lets us go on to develop new ideas without having to constantly reinvent the old ideas. Society moves forward by "not reinventing the wheel"!
IMAGE CREATED BY GAVS AND USED ACCORDING TO TERMS OF USE.