0:00

In this video, you will learn about conditions and branching.

Â Comparison operations compares some value or operand,

Â then based on some condition,

Â they produce a Boolean.

Â Let's say we assign a value of a to six.

Â We can use the equality operator denoted with

Â two equal signs to determine if two values are equal.

Â In this case, if seven is equal to six.

Â In this case, as six is not equal to seven,

Â the result is false.

Â If we performed an equality test for the value of six,

Â the two values would be equal.

Â As a result, we would get a true.

Â Consider the following equality comparison operator.

Â If the value of the left operand, in this case,

Â the variable i is greater than the value of the right operand,

Â in this case five,

Â the condition becomes true or else we get a false.

Â Let's display some values for i on the left.

Â Let's see the value is greater than five in green and the rest in red.

Â If we set i equal to six,

Â we see that six is larger than five and as a result, we get a true.

Â We can also apply the same operations to floats.

Â If we modify the operator as follows,

Â if the left operand i is greater than or equal to the value of the right operand,

Â in this case five,

Â then the condition becomes true.

Â In this case, we include the value of

Â five in the number line and the color changes to green accordingly.

Â If we set the value of i equal to five,

Â the operand will produce a true.

Â If we set the value of i to two,

Â we would get a false because two is less than five.

Â We can change the inequality if the value of the left operand, in this case,

Â i is less than the value of the right operand,

Â in this case, six.

Â Then condition becomes true.

Â Again, we can represent this with a colored number line.

Â The areas where the inequality is true are marked in

Â green and red where the inequality is false.

Â If the value for i is set to two,

Â the result is a true.

Â As two is less than six.

Â The inequality test uses an explanation mark preceding the equal sign.

Â If two operands are not equal,

Â then the condition becomes true.

Â We can use a number line.

Â When the condition is true,

Â the corresponding numbers are marked in green and red for where the condition is false.

Â If we set i equal to two,

Â the operator is true as two is not equal to six.

Â We compare strings as well.

Â Comparing ACDC and Michael Jackson using the equality test,

Â we get a false, as the strings are not the same.

Â Using the inequality test,

Â we get a true, as the strings are different.

Â See the Lapps for more examples.

Â Branching allows us to run different statements for a different input.

Â It's helpful to think of an if statement as a locked room.

Â If this statement is true,

Â you can enter the room and your program can run some predefined task.

Â If the statement is false,

Â your program will skip the task.

Â For example, consider the blue rectangle representing an ACDC concert.

Â If the individual is 18 or older,

Â they can enter the ACDC concert.

Â If they are under the age of 18,

Â they cannot enter the concert.

Â Individual proceeds to the concert their age is 17,

Â therefore, they are not granted access to the concert and they must move on.

Â If the individual is 19,

Â the condition is true.

Â They can enter the concert then they can move on.

Â This is the syntax of the if statement from our previous example.

Â We have the if statement.

Â We have the expression that can be true or false.

Â The brackets are not necessary. We have a colon.

Â Within an indent, we have the expression that is run if the condition is true.

Â The statements after the if statement will run

Â regardless if the condition is true or false.

Â For the case where the age is 17,

Â we set the value of the variable age to 17.

Â We check the if statement,

Â the statement is false.

Â Therefore the program will not execute the statement to print, "you will enter".

Â In this case, it will just print "move on".

Â For the case where the age is 19,

Â we set the value of the variable age to 19.

Â We check the if statement.

Â The statement is true.

Â Therefore, the program will execute the statement to print "you will enter".

Â Then it will just print "move on".

Â The else statement will run a different block of code if the same condition is false.

Â Let's use the ACDC concert analogy again.

Â If the user is 17,

Â they cannot go to the ACDC concert but they

Â can go to the Meat Loaf concert represented by the purple square.

Â If the individual is 19,

Â the condition is true,

Â they can enter the ACDC concert then they can move on as before.

Â The syntax of the else statement is similar.

Â We simply append the statement else.

Â We then add the expression we would like to execute with an indent.

Â For the case where the age is 17,

Â we set the value of the variable age to 17.

Â We check the if statement,

Â the statement is false.

Â Therefore, we progress to the else statement.

Â We run the statement in the indent.

Â This corresponds to the individual attending the Meat Loaf concert.

Â The program will then continue running.

Â For the case where the age is 19,

Â we set the value of the variable age to 19.

Â We check the if statement,

Â the statement is true.

Â Therefore, the program will execute the statement to print "you will enter".

Â The program skips the expressions in

Â the else statement and continues to run the rest of the expressions.

Â The elif statement, short for else if,

Â allows us to check additional conditions if the preceding condition is false.

Â If the condition is true,

Â the alternate expressions will be run.

Â Consider the concert example,

Â if the individual is 18,

Â they will go to the Pink Floyd concert instead of

Â attending the ACDC or Meat Loaf concerts.

Â The person of 18 years of age enters the area as they are not over 19 years of age.

Â They cannot see ACDC but as their 18 years,

Â they attend Pink Floyd.

Â After seeing Pink Floyd, they move on.

Â The syntax of the else if statement is similar.

Â We simply add the statement else if with the condition.

Â We then add the expression we would like to

Â execute if the statement is true with an indent.

Â Let's illustrate the code on the left.

Â An 18 year old enters.

Â They are not older than 18 years of age.

Â Therefore, the condition is false.

Â So the condition of the else if statement is checked.

Â The condition is true.

Â So then we would print "go see Pink Floyd".

Â Then we would move on as before.

Â If the variable age was 17,

Â the statement "go see Meat Loaf" would print.

Â Similarly, if the age was greater than 18,

Â the statement "you can enter" would print.

Â Check the Lapps for more examples.

Â Now let's take a look at logic operators.

Â Logic operations take Boolean values and produce different Boolean values.

Â The first operation is the not operator.

Â If the input is true,

Â the result is a false.

Â Similarly, if the input is false,

Â the result is a true.

Â Let A and B represent Boolean variables.

Â The OR operator takes in the two values and produces a new Boolean value.

Â We can use this table to represent the different values.

Â The first column represents the possible values of A.

Â The second column represents the possible values of B.

Â The final column represents the result of applying the OR operation.

Â We see the OR operator only produces a false if all the Boolean values are false.

Â The following lines of code will print out: "This album was made in the 70s' or 90's",

Â if the variable album year does not fall in the 80s.

Â Let's see what happens when we set the album year to 1990.

Â The colored number line is green when the condition is

Â true and red when the condition is false.

Â In this case, the condition is true.

Â Examining the second condition,

Â we see that 1990 is greater than 1989.

Â So the condition is also true.

Â We can verify by examining the corresponding second number line.

Â In the final number line,

Â the green region indicates where the area is true.

Â This region corresponds to where at least one statement is true.

Â We see that 1990 falls in the area.

Â Therefore, we execute the statement.

Â Let A and B represent Boolean variables.

Â The AND operator takes in the two values and produces a new Boolean value.

Â We can use this table to represent the different values.

Â The first column represents the possible values of A.

Â The second column represents the possible values of B.

Â The final column represents the result of applying the AND operation.

Â We see the OR operator only produces a true if all the Boolean values are true.

Â The following lines of code will print out "This album was made in the

Â 80's" if the variable album year is between 1980 and 1989.

Â Let's see what happens when we set the album year to 1983.

Â As before, we can use the colored number line to examine where the condition is true.

Â In this case, 1983 is larger than 1980,

Â so the condition is true.

Â Examining the second condition,

Â we see that 1990 is greater than 1983.

Â So this condition is also true.

Â We can verify by examining the corresponding second number line.

Â In the final number line,

Â the green region indicates where the area is true.

Â Similarly, this region corresponds to where both statements are true.

Â We see that 1983 falls in the area.

Â Therefore, we execute the statement.

Â Branching allows us to run different statements for different inputs.

Â