Introduction to Programming for Business Analytics - Lecture 3: Control Flow
This is a modal window.
The media could not be loaded, either because the server or network failed or because the format is not supported.
Formal Metadata
Title |
| |
Title of Series | ||
Number of Parts | 22 | |
Author | ||
License | CC Attribution 4.0 International: You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal purpose as long as the work is attributed to the author in the manner specified by the author or licensor. | |
Identifiers | 10.5446/64431 (DOI) | |
Publisher | ||
Release Date | ||
Language | ||
Producer |
Content Metadata
Subject Area | ||
Genre | ||
Keywords |
00:00
Computer animation
09:20
Computer animation
17:52
Computer animation
26:23
Computer animation
34:54
Computer animation
43:25
Lecture/ConferenceMeeting/InterviewComputer animation
52:43
Computer animation
01:01:19
Computer animation
01:09:54
Computer animation
01:18:29
Lecture/ConferenceMeeting/InterviewComputer animation
Transcript: English(auto-generated)
00:10
Welcome everyone to this third lecture of the introduction to programming for business analytics class in today's lecture We're going to talk about something called control flow the outline for today's lecture
00:22
Is to actually start by explaining what control flow is and this is by explaining something called the flow of execution Then we're going to move on to talk about Repetition repetition has two parts The first one is called while loop and the second one is called for loop And then we're going to move on to something called conditional execution
00:42
And finally, we're going to conclude this lecture by giving a note on something called debugging So let's go ahead and get started. So the first thing is we want to define what is the flow of execution? The flow of execution is actually the order in which the statesman the statements that we type in a computer are executed
01:02
so typically as it is the case for reading or writing any Language such as English or German for example Evaluation begins at the first sentence of the program just like it is in the first page So the first line of code and then statements are run one at a time from left to right
01:22
And then once we go at the end of the line, we move on to the next line So we keep going from top to bottom from left to right and so on Repetition and conditional execution, which is the two things that we are going to talk about today Allow us to control this flow of execution this idea of going from left to right and top to bottom and in particular
01:45
repetition allows to control how often certain parts of the code are executed and conditional execution control whether or not certain part of the code or certain parts of the code are executed this might sound abstract for now, but
02:02
It will become clear as we go through the remaining slides Alright, so let's start with a generic form for a control flow statement a control flow statement Which can either be a repetition or a conditional statement has the following generic form
02:23
so the first thing we have is this header here, which typically declares the start and Specifies the type of the control flow statement that we are trying to execute. Okay, that's the header here and the header also has a keyword
02:44
That indicates the type of the statement so the definition like we said here that it declares the start that we're gonna write a Control flow statement and then we specify that type of the statement We do this by writing a keyword that indicates what type of flow statements we want to
03:01
control flow statement we want to execute and then the keyword is Typically followed by another expression that depends on that type of the statement That's the first part and then there is the body of the control flow statement and the body contain expressions Or part of the program that's being controlled
03:23
Okay, so we'll talk about this in more detail but the body of this control flow statement is Actually contains all of the expressions that we are trying to actually control either by repeat or execute in a conditional manner for readability purposes
03:41
every expression in this body is typically indented and When we say indented we mean that there is this sort of extra space Between the beginning of the line and the start of the expression that goes through for all of the lines So just to keep in mind the body does not have to be a single expression In fact, there is no limitation on the number of expressions in the body, right?
04:03
and then there is this thing we call a termination expression which we indicate here by end and This termination expression indicates whether the control flow Where the control flow statement ends. Okay to do this. We're gonna use a keyword. It's called end Okay, just to summarize we have a header which tells us that we want to write a control flow statement
04:24
What type is it and read for that we use a header usually followed by an expression And there is the body which contains the part that we're trying to control and then there is the termination expression Which tells us that we are done from controlling that part of the program Like I said indentation is a blank space before the start of the sentence produced by a single tab
04:42
So whenever we create this extra space we press tab to create this indentation Indentation is actually not very important when it comes to Julia Like I said, we only use it for readability purposes But as we will see in the second half of the course for programs like Python
05:01
It's actually extremely important to have the indentation. All right, so let's parse this control flow Type statements a little bit further and in particular we're going to talk about the first part which we mentioned is repetition So repetition is when we evaluate certain expressions and typically we do that with some variation
05:21
We evaluate them repeatedly until a specified termination criterion is reached So we have this chunk of the code that we want to execute Repeatedly and typically that's going to be with some variation and we do that until we have this condition that's going to be satisfied and then we'll stop and executing that part repeatedly
05:42
Repetition is done using a loop statement. So a loop statement is an instruction that repeats until the termination criterion is Reached or satisfied and each round of repetition in a loop statement is called an iteration So this might sound abstract a little bit, but if you look at this figure, hopefully becomes clear
06:03
So typically we start the flow of execution from the first line of the program Let's say and we go through each line in the program one at a time What we typically do is we have this condition that Checks whether the loop statement will continue or not And the first thing we do is we check whether this condition has been satisfied or not
06:24
If the condition has been satisfied then we terminate the code. So we go in this branch which says yes, so we are done Otherwise, we're gonna go into the code execute that code and then go all the way back again here to the point Where we check the condition or the termination criteria we check again if it's satisfied we are done if it's not satisfied
06:47
We repeat executing the code until and then we go back to the condition again until this condition is satisfied And we go to yes and we terminate There are two types of loops. The first one is called a while loop and the second one is called a for loop
07:06
In a while loop the termination condition is based on a boolean expression or a bool value So if you remember from the previous lecture, we talked about bool values. We said that bool values are either true or false values So we have a true or false value or an expression that evaluates to true or false in that header
07:26
Which tells us whether the condition of that while loop has been satisfied or not The second time is called a for loop And the termination condition is based on a range or a sequence of values So unlike here where we have this condition. We have this range of values that we are going to iterate through until
07:47
We reach the end and then that's when we stop Like I said, some of them might sound abstract But we're gonna go through a couple of example and they will become clear in a second, but we have a question which is Why do we need to execute the same code repeatedly in the first place?
08:04
It's a natural question to ask once we talk about loops and The key actually goes to the heart of the reason why we use programming in the first place or computers in the first place Which is that repetition is actually very useful in repeating repetitive tasks without making errors So actually us as humans whenever we do repetitive tasks, we are very prone to making mistakes and
08:27
repetition Or this type of control flow statement where we actually repeat part of executing part of the code Allow us to do this without making the error Alright, so let's put this into context and take a look at the first type of
08:45
repetition or control flow statement that allow us to do repetition which is the while loop and we're gonna do this by looking at a very simple example where we have to In each new line a countdown from 10 to 1 so we just start from 10 9 all the way until 1
09:04
So we know how to print stuff and we know how to write Expressions that evaluates to numbers and if you're not familiar with while loops or any loops for that matter chances are you thought of writing something along the lines of this which is to say that
09:20
Because we want to print in a new line We're gonna write first is we're just gonna say printlin and then we can just write the first number as part of the countdown We go to the next line We just say ok printlin now 9 because we want to do a countdown from 10 to 1 and then we do printlin 8
09:40
and then we keep We keep doing the same thing for All the remaining numbers from 7 until 1 so You have here 7 you have 6 here and 5 4 3 2
10:01
I'm missing one here. So that's gonna be 8 And then one here and we don't need this guy here, okay Alright, so it's simply by just typing printlin Followed by the number we want as part of the count we type printlin because we want to print in a new number
10:21
So when I execute this cell as you can see here indeed, we get the countdown that we are trying to do but one of the key things whenever we think about writing loops or the concept of loops is that Soon as you see a pattern that repeats multiple times. This is where you can actually exploit this
10:42
one of one of the features of programs or writing programs in the sense that you can use it to Execute the same thing for you without having to type the same Part multiple times. So as you can see here, we have printlin, printlin We typed that 10 times the only thing that difference between the different lines is the number that we are trying to print and this is where a
11:06
Loop whether it's a while or for loop comes into play You might not find the print from 10 to 1 as tedious In the sense that we only had to type 10 lines But what if the countdown starts from 100 and what if the countdown starts from 10 billion?
11:27
As you can imagine This is not something that we want to do which is to write 100 lines of code or 10 billion lines of code that does exactly the same thing And the only thing that is different different between them is the number that we are printing
11:41
Using a while loop we can perform a countdown from any number in a concise manner So, let's see how we can do that. So I'll write it down first this blueprint For a while loop following the framework that we talked about earlier for a control flow statement And then we'll break it down one step at a time
12:01
So the first thing I'm gonna do is I'm gonna define this variable here I'm gonna call it count now and in this variable I'm gonna store the value which I want the countdown to start from okay So this is actually outside of the for loop I haven't started writing the while loop yet next I'm gonna type the keyword while which indicates that I want to start writing a while loop
12:24
That's the header and in the header. We also have the termination condition or the termination Criterion which is typically given by a boolean expression in this case What I want to do is I want to execute this loop Until this countdown reaches one, right? So while the countdown is
12:47
greater than zero meaning that I haven't Printed one yet. I'm still going to repeat the same thing again So this completes the header and then we can go to the body as you can see
13:03
Julia and the notebook actually is smart enough that it recognized that I'm writing a while loop and as soon as I pressed enter it immediately created the indentation that we talked about earlier The next thing I want to do is actually print lane, which what we did earlier But instead of writing the number explicitly what I'm going to do now is I'm gonna use the variable that I have which is
13:26
Count down. Okay Of course at the beginning countdown is going to be 10 So there is no problem here. At least I should expect to see the value of 10 in the next line because I want to ensure that this loop
13:40
Actually prints the following number and the following number and the following number What I'm going to do is I'm going to say count down Minus equals 1 this means that in every iteration or every time we go through the body of this conditional state of this
14:02
Repetition control flow statement, we're going to subtract one from the current value of countdown, okay, and then I go to the next line and then I just type in in to indicate that I Would like to end this control flow statement in this line, okay I execute the cell and as you can see here
14:24
We can get exactly the same thing that we wanted which is a countdown from 10 to 1 But let's go through that one more time But now we keep in mind the overall structure of what we're trying to do We have this value of countdown which we initialize to be 10 and as we mentioned earlier
14:42
we create a control flow statement by a structure where we have a header and a body and then a termination Keyword or a termination expression. The termination expression is straightforward. You just say that we went in the loop here and The header starts by a keyword that indicates the type of the loop or the type of the control flow statement that we're trying to create
15:05
Which in this case is going to be while then it's followed by the expression and Note that here We have countdown greater than strictly greater than zero meaning that as long as the value of this countdown strictly greater than zero What I'm gonna do is I'm just gonna go
15:21
Line three line four come back again line three line four until this value of countdown is no longer Strictly greater than zero at the beginning. It's ten. So I go here I go here But the next time after it's ten here the next iteration Soon as I arrived to line number four, I'm going to subtract one from it
15:43
So it's going to become one But when I check here nine is strictly greater than zero so I'm going to continue the loop one more time Okay, I print the new countdown value which is in this case is going to be nine. I subtract one It's going to become eight and then I go to the new line
16:02
I check is countdown still greater than zero or still strictly greater than zero Eight is strictly great is indeed strictly greater than zero. Therefore I continue the loop So I print the value of eight I subtract one seven seven is strictly greater than zero print countdown Subtract one six six is strictly greater than zero and so on until we get to one here
16:25
I get to one I've printed the value of one I subtracted one from one So I become zero as soon as it becomes here as soon as this guy becomes zero This means that zero is no longer strictly greater than the right hand side here
16:40
Which is zero which mean that this condition now is false Condition that is false indicate that the loop can or the loop will be terminated. Okay, that's the overall structure Of course, you might not see this as valuable But if we actually go into a new line here and we just copy and paste whatever we had from before we can actually
17:06
type in 100 here and hit execute and As you can see here, we have successfully printed 100 lines of code by just our 100 That values of countdown by just five lines of code
17:24
we can even stretch that by typing that extremely large number we had earlier and This can also be easily done here And as you can see we are able to print all the numbers But Julia is actually taking some time to process all the numbers so to terminate this
17:43
Procedure we can actually just click this guy here to interrupt the kernel which is actually allowing Julia to be processed So that we can continue with the lecture, but you get the idea so now it changed to six so star usually indicates that and the cell is still being executed and Whenever you have a number it tells you the order of that number or the order in which the cell has been
18:06
Executed so let's just write down 10 again to just keep things concise or maybe we can even have 100 here and we can just hide the output of the cell by
18:21
Typing or by double pressing on the left side here. So if we double press on the left side here You can completely hide it as well and then move on to the next line All right, so we've just looked at the while loop, but let's actually break it down even a little bit further
18:41
And in particular we're gonna look at the header because that's the really the heart of writing a while So we said that a while loop statement has the following generic form We typically have this while to indicate that we're trying to write a while loop It's a keyword and then we have the in expression at the bottom here to Indicate that the loop ends here. Typically the while loop
19:04
Has a header that is where the keyword is followed by a boolean expression that boolean expression is basically what? Tells us the termination criterion that we have in the previous example. That was the fact that countdown is still Strictly greater than zero, okay
19:21
So the header consists of the keywords followed by boolean expression the boolean expression defines the stopping condition Recall that a boolean expression evaluates to a bool value. So it's either true or false So every time whenever a countdown was strictly greater than zero It evaluated to true and the moment we had countdown to be equal to zero zero is not strictly greater than zero
19:44
Which was the right hand side and therefore we evaluated to false and therefore the loop stopped So as long as the while or as long as the boolean expression Evaluates to true the loop will continue and as soon as the boolean expression evaluates to false the loop will be stopped
20:05
In other words this while loop in some sense to read that as English we can read the whole expression that we have here or this generic form that we have here by Saying that while the condition while the boolean condition is true
20:21
Evaluate the body of the code when the condition becomes false we stop Alright, so you might be wondering where do we use something like countdown? Which is in the sense that it's not something that we encounter in a day-to-day Applications and to put loops into perspective. We're going to talk about something called in the Newton's method
20:42
So loops are often used in program that compute Numeric results by starting with an approximate answer and iteratively improving that answer. So explain what that means in a second for example one way of computing the square root of a number say we call that number a Is to use this Newton's method and what we do in this Newton's method is we start with almost any estimate
21:06
It can be any value for your guess of what is the square root of a is Okay, so we want to find the square root of a we just guess any value for that That's is X 0 and then we improve that estimate or that guess Using this following formula. I'll say the formula now and we will break it down in a second
21:25
And the formula is as follow. It says that X K plus 1 equals 1 over 2 multiplied by X K Plus a divided by X K, okay X K indicates the value of the estimate after K iterations. Okay, so we're gonna do this repeatedly
21:44
At first we're gonna have an estimate. It's gonna be X 0 That's our initial estimate or initial guess and we plug that into the formula. So 1 over 2 Multiplied by X 0 plus a divided by X 0. Okay, that's gonna give us a number and that's gonna be X 1
22:02
Right, that's in the first iteration. Okay. It's okay. The first iteration is equal 1 We take this new value of X X 1 we plug it back into the formula So we have 1 over 2 multiplied by X 1 plus a divided by X 1 Of course, we just calculated X 1 by using X 0 But now we have a new value and this new value tells us what X 2 is because we're doing that for the second time
22:26
That's the second iteration And the new X 3 what we use the new X 3 we plug it back into the formula We get X 4 and so on we keep repeating this process repeatedly Until we get what we want which is the square root of the value a okay in other words
22:44
we do this repeatedly until this estimate that we have the guess that we have is equal to the value which is equal to the square root of a Okay. All right. So let's see how we can do that So the first thing I'm going to define the value of a for which we're trying to calculate the square root
23:03
Let's just assume that we want to calculate the square root of say 81 Of course, we know that the square root of 81 is 9 But we want to find that using this Newton's method and in particular using a while loop Okay The next thing is we're going to initialize the value of X and we're gonna set that to a very small number
23:23
Like I said earlier, this is just a random guess. It can be more or less any number. Okay? But now what we're going to do is we're just going to define a value Y And we're gonna set that value Y to be equal to 1 over 2 multiplied by We open the parentheses X plus a divided by X
23:45
This is actually exactly equal to the Newton's method formula The only thing that is different is that we don't have this case subscript And on the left side we have Y instead of X K plus 1 Reason for that is just because we don't want to write X K plus 1 because this will make and the syntax a little bit
24:04
complicated But just for simplicity, we just assume that X K plus 1 can be represented by Y here, okay? Nevertheless what we have here is we say that Y is equal to 1 over 2 Multiplied by X plus a divided by X. Okay, so that is the general setup
24:21
But now let's write the loop itself Okay First thing we have is the while the keyword while which indicates that we want to write a while loop And we're gonna have a boolean expression here And I'll write the boolean expression now and I'll explain it in a second and the boolean expression is while Y not equal to X and
24:42
The first thing that I'm gonna do is when I enter the loop is I'm gonna set X to be equal to Y and you'll see why we do that in a second and then we're going to update Y using the formula that we have written above so 1 over 2 Multiply by opening the parentheses X plus a divided by X. Okay, and
25:03
Then we end the loop here So, let's see what we did We have a header which followed by a which has the keyword while followed by the boolean expression and in the first line We set X equal to Y. Okay Suppose that we don't have this line number 7 for now assume that we just don't have this guy at all
25:23
What's gonna happen is that we say that while Y is not equal to X But the first thing we do as soon as we enter the loop is that we set equals X equal to Y So if the next time we come back to the boolean expression that checks for whether Y is not equal to X
25:42
or not Evaluates to true we're gonna value we're gonna execute the body of the Statement again, and if not, we're going to or if it evaluates to false we're going to terminate the code But here when we have X when we set X equal to Y and nothing happens after that
26:00
we expect Y to be equals to X and Therefore this condition will evaluate to false because Y in that case would indeed be equal to X okay, but this is not the case because we have another expression here that says that Y is going to be updated to 1 over 2 multiplied by X plus a
26:21
Divided by X and what we're doing here is that we're using the Newton's formula that we've defined above So what is happening is that as I mentioned earlier, we have a value of X we use that to need to obtain a new value X K plus 1 and we were using here Y as X K plus 1 So when we set the value of Y or when we set the value of X to be equal to Y
26:45
This is equivalent to saying that You want to set the value of the left-hand side or the new value of X to be equal to whatever we calculate here We calculate Y using the updated value of X Okay So if you look at the right hand side here when we plug in X this gives us X K plus 1 which is in
27:05
This case Y we use Y to update X here again We're gonna keep doing this procedure until this value the value of Y or X Are exactly the same and then we're going to terminate So the idea is that this procedure repeats or this procedure repeats updating the value of X
27:26
continuously until we reach the desired answer, which is the square root of a and we know that this is Going to be not and to see that we're just going to add a print statement here that says print the value of X
27:44
Okay So let's execute the cell And as you can see here The first iteration we have this very large number and then it's getting smaller smaller and smaller and smaller And it's getting smaller and smaller and it's getting even smaller here in the sense that it's very close to 9
28:04
But it's not exactly 9 and it's getting slower smaller or more and more towards 9 Until it exactly reaches 9 Okay, if you actually remove this condition here and just write while true
28:22
Okay, you'll see that after it reaches 9. It will never change. Okay, and as you can see here It just says 999 so it's never changing at all That's why we know that and we can terminate as soon as the value of X And does not change from the previous iteration and as you can see here
28:42
Julia is still executing because we don't have a termination criterion, which was something we'll talk about in the future all right, um before we move on to the second type of loops and One important thing to note that is that we can actually also write Loops that are nested within each other. What we mean by that is that we can have a while loop within a
29:06
Another while loop. So in this example we have the different types of suits within a Deck of cards and we have or we know that each suit has different types of cards So in particular we have 13 different cards within a particular suits and this is the same for all four
29:25
Okay, and what we're doing here is that we're iterating through all of the different suits and all of the different elements within a Particular suit and we want to do is that we simply we just want to print the value of the suits Which is the symbol corresponding to that actually and then the number corresponding to it
29:44
Okay, I will not go through the codes in detail and you can play around that with that at home But just to give you the brief idea We have this outer loop Which is the bigger one that goes through all of the different suits and then we have an inner loop that goes through the different elements within the suit
30:02
the other one goes from one up until four and we know that because The stopping criterion here says that while suit count is strictly greater than length suits Length suits tells us that this guy is four. So this is gonna do this Repeat this process four times and we know that because every time within the while loop within the outer branch
30:27
We are incrementing the value of suit count by one. So suit count is something that we initialize here. Okay For every time we encounter and you over every time we go through the different suits. We're gonna have
30:41
Another variable which is called ranks count and that we initialize as zero and this guy is gonna go from one all the way until 13 every time within the inner loop we increment ranks count by one and in that case We have different combination for suits and the corresponding number
31:03
So let's execute the cell and as you can see here the first time we have spade Number of course the number next to it is one two three all the way until 13 because we reached 13 here We're going to end the first loop. We're gonna come back at the beginning and now it's gonna be with different numbers That's two and after at that we have diamonds and then we have one to 13
31:25
and then we have the next type which is hearts and then we go to 13 and then we get to The last type which is clubs and then we also go until 13 and then the loop finally stops So let's now talk about the second type of a loop statement
31:44
Just called the for loop and we're gonna do that by looking at the simple countdown example We covered at the beginning so if you remember for the while loop the first thing we did is that we initialize the value of count and to be And then we wrote the header of the while loop followed by the body and then followed by the termination key word
32:04
We'll do the same. I'll write it down first and then we go through it together So I'll write down four and I'll write I in one Column 10 and in the new line, I'll type in print lin Countdown which is the value of the countdown at that iteration and then in the new line, I'll write countdown
32:28
Minus equals one and then finally I will close the loop statement, okay If I hit execute, you'll see that we get exactly the same as we did before
32:44
One thing to note that this statement is actually more or less similar or more or less exactly the same as the one that We had for the while loop statement. The only thing that is different is actually the header Okay, which we're going to break down in a second
33:01
So the for loop statement has the following generic form It starts with a K The header starts with this keyword for and it's followed by this thing We called an iterator in the previous example That was I and then this iterator is followed by another keyword Just called in and this in is followed by something called an iterable sequence
33:23
Okay This Iterator is actually a variable which changes its values In each of the different iterations depending on the iterable sequence that we have So what is an iterable sequence? An iterable sequence is an ordered collection of elements that we can iterate over
33:45
We're going to discuss this in More details based on three types actually a range string and a vector But in the simple countdown example that we just talked about All you need to know is that this iterator variable this I takes value in this
34:00
iterable sequence Which explain why we use the word in here this iterable sequence that we has we have uses the values 1 2 3 all the way until 10 Okay, the way we define that is by tapping 1 column 10 as we did in the example Okay, just a quick note we can actually write equals 2 instead of n but both syntax suffice for Julia
34:26
So let's talk about these iterable sequence because they really are the heart of four loops And we're going to talk about them Like I said in three types and the first one is called a range in the previous example The iterable sequence is given by a range
34:43
Mathematically speaking a range is a sequence of elements uniquely determined by a combination of the following parameters We have a start value Which is the first element of the sequence in the previous example That was 1 and then we have a step which is the different or the space between two consecutive values in that sequence
35:04
In the previous example, that was also 1 because between 1 and 2 Difference is 1 or the step is 1 between 2 and 3 The step is 1 between 3 and 4 the step is 1 and so on all the way until the difference between the last two numbers, which is 9 and 10 and that difference is 1 and
35:22
Then finally we have a stop which is the value of the last element in that sequence Okay, so a generic range in Julia has the following form in the previous example We just write 1 and then we write actually the start and the step only but we don't write the step And this is because when the value of step is not specified Julia assumes that it's actually equals to 1
35:45
So we didn't need to specify that However, we can have a more complex range where we have different types of iterable sequence Where it's not necessarily increasing by 1 between or difference between two numbers
36:01
Or two consecutive values in the sequence is not necessarily 1 So in the following example What we're going to do is that we're going to start by 1 and step 3 and stop at 10 This gives us an iterable sequence that has the following elements So 1 that's the first start the first element in the sequence then 1 plus 3 because we're always stepping by 3
36:26
So it's gonna be 4 and then we're gonna have 1 plus 3 which is 4 from the previous step plus 3 That's the new element. So it's gonna be 7 and so on until we get into 10, okay So to do this we can
36:43
type 4 i in 1 3 10 Then you align we type in print lin The value 5 and then you just close the for loop statement and we
37:03
Hit execute. So as you can see here, we have 1 4 7 10 One thing to note is that neither start or step has to be an integer value So although we're using 1 3 10 here, which are integer values It's not necessarily that we have to have an integer value or a positive integer value for that matter
37:25
So here are a couple of examples so in this example we have for i in Let's say 0.25 colon 1 colon 5 and then We print then the value of i and then in the new line
37:44
We just close the statement by typing int. Okay, and as you can see here the sequence is actually Starting from 0.25 rather than starting from an integer number And similarly, we can also have a different example where
38:01
not only that we start with 0.25 but we can start with 0.25 and we actually we also increment by 0.5. Okay, and We int the sequence instead of at 5 we ended at 4.99 Okay, so we execute this you see you start 0.25 we increment 0.5
38:25
0.75 and so on and now we don't stop at 5 because the largest number here is actually a point is 4.99 so if we increment 0.25 here this will violate the Largest value in the sequence here, which must be less than or equal to 4.99 and therefore
38:45
5 will not qualify by the same token Neither start stop or Step has to be positive so we can write something like for i in
39:01
negative 5 colon 2 Let's say colon 2.5 and then colon 5 and then in the new line we type print then i And then We int the forward loop. If you execute the cell you see that instead of starting from one or a positive number
39:22
We're starting actually from a negative number, okay, and This can be very useful because if you remember from the countdown example if we were to write for i in 10 Colon
39:41
minus 1 1 here and then printlin i and then we Close the statement We actually get exactly the countdown example without having to initialize the variable countdown and subtract every time from it So this is completely handled by the iterable sequence that we define here
40:04
So it's essentially an iterable sequence that increases by step or that decreases by 1 every time that gives us the countdown that we were trying to calculate by typing countdown minus equals 1 Alright, so not only that we can have
40:20
Numbers as iterable sequence, but we can also use string values as iterable sequence So in this example in each iteration the iterable variable that changes the values and its value in each iteration Will take values of different characters comprising a string so to see this in an example say we write for in
40:42
And instead of writing something that or a number then colon then another number and so on We're gonna write a string value. Let's say we write hello world, okay, and then we say I'm missing an I here. So we say print Lin the value of I
41:04
And then we type int to close the loop statement if we execute the cell you'll see that in each iteration it printed the value of the character or all the different characters in the string that we have We have a comma here. That's the last character after the word
41:23
Hello, and then there is actually a space which explains why we have here. So if we Remove the space as you will see we don't have the space anymore If we removed also the comma You'll see that and we don't have anything between the last word in the the last
41:42
character in the first word and the first character in the second word similarly, we can also iterate or use vectors as iterable sequence So in this example in each iteration the iterable variable takes values of the different elements inside of a vector
42:01
So say for example, we write something like for con In and we define a vector. It has values 1 2 100 200 and let's say I 99. Okay, and Again, I'm missing an eye here. So if I type print then I and then I close the loop here I
42:28
Execute as you can see we have 1 to 100 200 and 999 Just like we were able to nest while loops within each other
42:41
We can also nest for loops within each other so we can write one for loop within another and we can see that here by using the same example by where we iterate through the different types of Suits and different elements within or the hidden ranks within a particular suit We can do that by even a more concise syntax using the for loop statement by simply writing for suit in the list of suits
43:05
And then for rank in 1 to 13 and simply typing suit rank here and directly Executing the cell you'll see that we get all the values of the different suits and their corresponding rank So you can also play around with this code at home and we will not go through it in details
43:25
So let's continue with the second part of the lecture we talk about conditional execution so suppose we want to perform a Countdown or the countdown from 10 to 1 but only display the value of countdown when it's even to check if a number is even it's actually equivalent to check whether it's divisible by 2 and
43:46
If you want to check whether a number is divisible by 2 we can use An operator called the modulus operator Which is defined by using the percentage sign and it's a binary operator that gives the remainder of dividing Two numbers in particularly the number to its left and the number to its right. So let's take a look at a couple of examples
44:07
so we're gonna do print clean here and Then between the parentheses we're gonna say what is the remainder of the my dividing 3 by 1?
44:20
And just to see how it is, of course between whatever is between the quotation, right That's just gonna be the string that is printed and that's the actual thing that we want to print. Okay, and If I execute the cell You'll see that it says that the remainder of dividing 3 by 1 is 0 because 3 is indeed divisible by 1
44:41
Okay, so there is no remainder of the division. So let's do the same thing But now let's try a different number so for example, maybe we can do 4 divided by 2 Here and 4 divided by 2 here and then in the next line, we also do something similar
45:05
Maybe we say 5 divided by 3 And then here 5 Divided by 3. So these are just a couple of examples to illustrate How this modulus operator works if I execute the cell you'll see that for the first two
45:22
3 is indeed divisible by 1 the remainder is 0 4 is indeed divisible by 2 the remainder is 0 However, our 5 is not divisible by 3 and the remainder is 2 So if X is divisible by Y then X Modulus operator Y is going to give us 0
45:41
Therefore to check if a number is Even we just need to check if it's divisible by 2 or not. Okay using the modulus operator So now we know how to check if a number is even And what we want to do is we want to figure out how we can tell Julia to only print countdown value if it's even and
46:01
To check if the value of countdown is even before printing it we use conditional execution So that is exactly the purpose of conditional execution and in particular conditional execution Gives us the ability to check if a certain conditions or if certain conditions are met before Executing the code the simplest form of a conditional execution is called the if statement
46:25
So let's take a look at the if statement using the countdown example that we just discussed So the first thing we do is we define countdown to be equal to 10 So we just initialize that as follows and then we say if countdown If countdown the value of countdown when we apply the modulus operator and then followed by two
46:45
Equals equals zero because that's how we check for equivalent or for equality. I should say then print lin the value between parentheses or in a string we say that count down and is
47:01
So that's the only thing that we want to check okay And then if I execute the cell you'll see that it says countdown is indeed even okay That's the first part But if I change this Countdown value the initial countdown value here instead of 10 to 9 and I execute the same thing again
47:20
You will see that nothing is being output. This is actually part of the text that is I'm already prepared in the lecture So just to to confirm that if I re-execute the cell you see that there is no output compared to the first one This is because whenever this condition was evaluated it evaluated to false and therefore nothing inside this conditional
47:43
Execution our inside the body of this statement has been evaluated. Okay Alright, so putting the whole thing together. What we can do is we can say that Initially we have that countdown Equals to 10. That's how we initialize it. And then we say while
48:04
countdown is strictly greater than zero that's what we've done in the beginning and Instead of directly printing the value of countdown like we did before we add this if statement that we have just created, right? So we'll just remove this here
48:22
What I've pasted from the previous slide and then I create the indentation for readability and Then I say that instead of print countdown is even of course What we want to do now is actually print the actual value of countdown. So we say count down here
48:40
Okay And then at the end, of course, we need to make sure that we subtract One from the current value of countdown and then we just closed the while loop statement By the end keyword. Okay, so just to go over all the entire structure So initialize the value of countdown as we did for the while loop
49:00
We write the while loop as we did before so nothing changes here The only thing that is difference is that instead of printing the value of countdown directly what we do is that we say if countdown is divisible by 2 which is Translated by this statement here, then we're gonna print the value of countdown. Okay, so
49:22
Let's see what happens if we execute the cell and as you can see here We have 10 8 6 4 2 and only the even numbers were Displayed in the screen. All right So let's just deconstruct this if statement or the simplest form of a conditional statement that we've just seen and in particular
49:41
We're gonna deconstruct the header Obviously as you've seen that if statement has a similar structure to the while loop and the for loop Whereas we have this header we have the body of the statement and then we have in an if statement the header consists of the keyword if and Then that keyword if is followed by a boolean expression
50:01
The boolean expression is the condition that determines whether the body of the statement is going to be Executed or not in the previous example that boolean expression was whether and countdown value is divisible by 2 Which essentially translates to other countdown value is an even number or not Okay So if the boolean expression evaluates to true the body will be executed and if the boolean expression is false
50:25
The body will not be executed Alright, so as I mentioned earlier the if statement isn't the only conditional execution statement that we can use to motivate this Next type of conditional execution. Let's take a look at an example Suppose that we would like to run something else if the condition in the header
50:44
Has not been satisfied. So in the previous example, we only print something if the value of countdown is even but what if we also want to print or do something else in the case the value of countdown is Not even so in this example instead of checking whether the countdown is divisible by two
51:02
we're gonna check if it's divisible by three and If it's divisible by three, we're gonna print the value of countdown, but if it's not divisible by three We're gonna display a message stating that the number is not divisible by three as well as the remainder of the division Okay, so let's do that. So first thing we do is we write down countdown and equals to zero
51:25
Or equals to ten render. That's what how we initialize it and then we say while countdown is Is strictly greater than zero what we want to do is you want to check if Countdown, but now we're obviously checking for the remainder of dividing by three is equal to zero
51:44
That's similar to what we did before We're gonna do is we're just gonna print the value of countdown So nothing changes so far compared to what we had from before But now instead of closing the if statement directly We're gonna type in else as if to say that we want to do something else and then what we're gonna print here
52:05
We're going to say that Countdown or the current value of condom when we divide that by three That's gonna equal to this value here count down
52:22
Three which is not divisible by Okay, of course as we did for the if statement we have to close this alternative conditional execution by typing the end keyword
52:40
Finally, we need to make sure that we subtract one every time we go through an iteration here And finally we close the while loop itself Okay, so in order to see how this line number six looks like let's execute the cell and see what happens So as you can see here ten is not divisible by three and actually the remainder is one so this implies that
53:04
Not divisible by three or a countdown is not divisible by three. Maybe you can also add here saying that the countdown So that we just see the value directly Is not divisible by C by three as you can see here It says ten not divisible by three nine is indeed divisible by three
53:21
So we just print it as it is eight divided by three and equals to two The remainder is two and therefore eight is not divisible by three and so on So alternative execution is a form of conditional execution where we have two possibilities or alternatives Each of the two possibilities is called a branch and the condition
53:43
I either boolean expression determines which of the two branches is executed So in the previous example, this is one alternative and this is one alternative. So both of these guys are called branches That's the first branch and that is the second branch The boolean expression or the condition in the boolean expression off in the F header
54:03
Determines which one of these gets determined So here if this evaluates to true the first one will be evaluated and the second one will not be evaluated And if it evaluates to false the first one will not be evaluated and the second one will be evaluated. Okay? The third type of conditional execution we're going to talk about is something called chained condition and so if we have
54:26
More than two possibilities we can use this type of conditional execution It's called chain conditional and the idea is pretty straight forward Especially coming from alternative execution in the sense that instead of having only two possibilities or only two branches
54:42
Conditional or chained condition executions allow us to have multiple okay, so if I go back here to the example that we had from before and I just copy everything that we have there and
55:01
Then paste it here. What we want to do is that instead of having only two branches the first one in the second one We're gonna create a another one. Okay, and in this other one We're gonna check if the value of countdown is divisible by two or not So let's write that down and we go through it together. So what I'm gonna write here
55:21
Between the if and the else I'm gonna write something called else if okay And this is going to be followed by countdown Divisible by two or modulus operator two equals equals zero and then here I'll just say print thin
55:41
and then in a string I'll type in countdown or the value of count on is Divisible by Two, okay So what I've done here is instead of having only two branches. I've created a Third branch now. I'm not only printing the value of countdown when it's divisible by three I'm also printing another statement that says
56:03
Print the value of countdown or print a statement saying that this value of countdown is divisible by two Whenever this condition is satisfied To achieve this I added this else if keyword here This in some sense can be read as if it's English
56:21
So what we're saying here is that if countdown is divisible by three then print the value of countdown else if Countdown is divisible by two do this else do this Okay Now you might notice that there is a crucial distinction here between else if and else Of course in addition to the syntax that else if is actually followed by a boolean expression
56:44
Whereas else is not this is because else is Basically everything that has not been addressed in the previous condition in each of the boolean expressions However, when we say that else if it's as if to say that if something else happened and we need to specify what that
57:02
Thing is and we do this by as we did in the simple case where we only have if We did that by writing a boolean expression following the keyword. Okay, so let's execute the cell and see what happens So as you can see here, it says that ten is divisible by two
57:20
Okay, so the first the previous time it directly said that ten is not divisible by three But now we have we allow ourselves also to not only print numbers that are divisible by three But also numbers that are divisible by two. So N is indeed divisible by three So it was directly printed and then here for a number that is not divisible by two and a number that is not
57:44
Divisible by three which is in this case seven. We have the last alternative or the last branch which says that countdown divided by three has this domain remainder and It is not divisible by three So in a chained conditional statement conditions are checked in order
58:03
What I mean by that is if the condition in the first branch is false The next branch is going to be checked So if you go back here, if this first expression here evaluates to false This is going to be evaluated. Okay, this else if part is going to be evaluated and
58:24
If the condition in any of the branches is true the body in the corresponding branch is going to be executed and the statement Will end directly and if we have more than one condition that is true only the first branch is executed So let's go back here So what this saying is that if you have two statements here that each one of them has a boolean expression
58:44
Because we evaluate things wonder that or one line at a time going from top to bottom so if the first statement evaluates to true the expression inside the body gets executed and Even if this else if would have also evaluated to true the body inside here would not be
59:03
Executed this is because as soon as we go inside the body of the first branch The statement will end directly after that. So this will not be evaluated This will not be evaluated But if this fails to evaluate to true meaning that it evaluated to false we're gonna go to the next one We check that if it evaluates to true we evaluate this guy and we're done if it evaluates to false
59:25
We're gonna go to the next one and so on So the last type of a conditional execution we want to talk about is called nested conditional execution Which is just another way to create a conditional execution statement that has more than two possibilities or more than two branches
59:43
Okay, and to do this what we're going to do is we're going to nest two statement or two conditional state conditional execution statements within one another So, let's see how we can do that for how we can replicate the example from the chained condition
01:00:00
by using a nested conditional so as we did before first thing we do is we initialize countdown to be equal to 10 so nothing new here and then we say while if I can type correctly while countdown strictly greater than 0 we want to print if countdown is divisible by 3 equals 0 here and we
01:00:26
want to print simply the value of countdown as it is. We haven't done anything new so far but now instead of doing else if and then writing the
01:00:40
expression or the condition that for which we want to create the second branch we're gonna just say else right it's I guess it's similar to the alternative execution that or the second time that we discussed okay and in this else our branch what we're going to have are we going to have another if
01:01:00
or another alternative execution statement so we're gonna say if countdown is divisible by 2 which is the second branch of the chain conditional but we're going to print is that this value of countdown is divisible by 2
01:01:24
okay of course we're not done here because we have a third branch and in that third branch what we've printed is we've simply printed this long statement here which I'll just copy from here and paste directly in
01:01:43
here okay and of course we need to make sure that we close each of the alternative execution statement that we have this is the int keyword for the first one and then we have another int keyword for the second one and of
01:02:01
course we need to make sure that we subtract one from each iteration of count down okay finally we just closed the while loop and if we execute the cell as you can see we see we have the same result as we did using the
01:02:24
alright so you might have noticed that each of the boolean expression we've discussed in the previous example consisted of a single clause only so what do I mean by that in the previous example we check for things like countdown is divisible by 2 or not or we check for something like countdown is divisible by 3 or not and so on but what if you want to
01:02:43
display the value or what if you want to check if countdown is divisible by 2 and divisible by 3 at the same time in other word we want to print or display the value only if it's divisible by 2 and 3 another example would be if we want to display the value of countdown if it's divisible by 2 or
01:03:03
divisible by 3 in order to do this or in order to explain how we're gonna do this in programming we need to discuss something related to proposition logic and truth tables and the first thing we need to introduce is the three symbols that can be used to alter or change the meaning of a
01:03:22
statement so we have free the first one here is equivalent to saying not the second one is equivalent to saying and the third one is equivalent to saying or to see how these work or how they can alter the meaning of a statement let's take a look at a couple of statements so the first one here states that the earth spins on its axis and the second statement is
01:03:43
that the earth is round okay so when I put the not simple prior to the letter that indicates the first statements P the statement would read as the earth does not spins on its axis in other word this symbol here negates the actual statement itself so if this is says it's spins on its actually will
01:04:02
say that it's not spinning on its or it does not spin on its axis if I use the as symbol and I put that between the P and Q I can read the statement as the earth spin on its axis and the earth is round if I put the or symbol it reads as the earth spins on its axis or the earth is round okay so
01:04:25
the truth table for the not or the negation symbol is as follows if I have Q here to be a true statement when I put the negation symbol prior that it becomes false if I have a false statement and I put the negation symbol
01:04:42
prior that it becomes true the truth table for the and symbol is as follows if I have two statements of course you can think of the and and the or symbols as binary operators whereas the negation symbol as a unary operator so we need for that because it's binary we need two statements so we need P and
01:05:04
Q so the truth table for the and symbol is if the first statement is true and the second statement is true then P and Q is going to also be true the first statement is true and the second one is false the P and Q is
01:05:20
going to be false and if the first one is false and the second was true it's going to be false and if both of them are false it's also going to be false. This is to say that if I have a statement that states the earth spins on its axis and the earth is round when I put the and symbol between the two it's as if I'm saying the earth spins on
01:05:47
its axis and the earth is round. But if I have a first statement here that is true and second statement that is not true when I combine them together the whole statement is going to be
01:06:02
false. So as if to say the earth spins on its axis and the earth is not round so the entire state can only read that it's going to read as false. And similarly if any of the two is false or if the first is false and the second one is true the entire the combined statement when
01:06:22
using the and symbol is going to read as false. In other words if any of the two statements is false the combined statement using the and is going to evaluate to false. On the other hand if I have the or symbol the first one is true and the second one is true I get true if the first
01:06:43
one is true and the second one is false I get true the first one is false and the second one is true I get true if the first one is false and the second one is false I get false okay. In other words if any of the two statement is true I'm going to get true here. If you zoom out
01:07:03
and compare the two to each other the and symbol in order for the combined statements to be true I must have both of the two statements to be true in order for the combined statements to be true. But for the or statement or for the statement that uses the or symbol
01:07:21
if any of the two satisfied I have the combined statements to evaluate to true. So it might not be straightforward to wrap your head around how when we have true and true we get false and when we have true or false we get true whether using the or and the and symbol and to do this we're going to use some maths. So in julia the symbol for not is the explanation mark
01:07:48
symbol for and is this to and here and what we use to and and for or is these two sticks and as I mentioned to help us wrap our head around this true and false for different situation
01:08:04
we're going to represent false as zero and we're going to represent true as one okay. And if I have p here and I negate that that's going to be equivalent to one minus p and if I have p and q that's going to be equivalent to saying p multiplied by q and if I have p or q
01:08:24
that's going to be equivalent to the minimum between p plus q or one or and one okay. So see how this fits with what we had from the truth tables for each of the different symbols so for the first one here negating p if I have p to be true I know that true is represented as
01:08:47
one then one minus p is going to be one minus one which is equal to zero which is essentially false because that's how we represent it here. So negating a true always gives us a false by contrast if I have p here to be false I know that this is zero so one minus zero it's going
01:09:07
to be one of course and one here we represent that as true so negating a false gives us a true so that is straightforward but now let's look at the truth table for p and q or saying p and q
01:09:21
and we said that this is equivalent to multiplying p by q. If you remember from the truth table we said that if any of the two statements is false the entire the combined statement will evaluate to false so if I have true and true here this is to say that one multiplied by one which is going to be one and one represents true but if any of the two is false I'm going to have
01:09:45
a zero here and multiplying anything by zero gives zero which is a false. On the other hand the or part which is a little bit tricky if I have any of the two to be true I know that the combined statement evaluates to true if I have any of the two to be true what that means is that
01:10:04
the minimum between one here or something that is at least one right because if I have any of the two to be true that's either going to be two or one so it's one plus one one plus zero or zero plus one so I'm actually considering the minimum between one or something that is at least
01:10:21
one and one which is always going to be one but if both of them are false I'm gonna have the minimum between zero plus zero which is zero and one and the minimum between the two is going to be zero okay one one one here as we represent up there is always is true true true and false
01:10:44
so now we come back to the example or the hypothetical situation that prompted this entire discussion about truth tables and and so on okay what we want to do is you want to print the value of countdown if it's divisible by two and it's divisible by three at the same
01:11:03
time so let's do that so first thing we initialize countdown to be 10 we write in the next line while countdown is strictly greater than zero we're saying is that if countdown modulus operator two equals equals zero that's the first cause but now we add this and
01:11:30
okay and following the end we're saying that countdown modulus operator three equals equals zero okay this is the condition we're checking for we're checking for two things now
01:11:41
we have print lin countdown and that's the value of countdown we want to print of course we're in the statement here we subtract and count down we subtract one and then finally we close the while so let's see what happens when we execute the cell we have a typo here
01:12:05
and as you can see here the typo is happening in this last line here so if i execute the cell the only value of um countdown that get x or that gets displayed is six so if you go through
01:12:20
the number well 10 is divided divisible by two but but it's not divisible by three nine is divisible by three but it's not divisible by two and eight is divisible by two but it's not divisible by three and so on so if you go through the numbers you'll see that six is the only number that satisfies the two conditions okay to do the same thing but now we just want to print or we want to print if any of the two is divisible if any of the two
01:12:45
conditions is satisfied in other words we want to print the value of countdown if it's divisible by two or if it's divisible by three and we do this by adding the two sticks that tells
01:13:01
julia that we are using the or operator okay now if i execute the cell you'll see that we have 10 that's divisible by two nine is divisible by three eight is divisible by two seven is not divisible by two and three and therefore it's not showing six is divisible by both two and three and four is divisible by two three is divisible by three and two is divisible by two all right so
01:13:27
an important concept in programming is something called debugging so programming errors are called bugs and the process of tracking them down is called debugging and there are actually three kinds of errors that can occur in a program the first one is called syntax errors and syntax
01:13:45
refers to the structure of the program and the rules about the structure so for example if we want to use parentheses they always come in matching pairs so i can say that one plus two is legal however writing eight preceded by parentheses without having a parentheses before that is going
01:14:05
to give us a syntax error but if there is a syntax error in the program julia will display an error message and quit so you will not be able to run the program that's the most important thing about syntax errors is that whenever you have them the program will not run at all
01:14:23
that's the first type the second type is called runtime errors and runtime errors do not occur until after the program has started running so for example if you want to find the square root of a negative number you're going to get an error so let's do that so if i say
01:14:43
square root what is the square root of say negative one as you can see here i'm gonna get a something called a domain error okay and that domain error is saying that something bad has happened and in particular we call that an exception because it usually indicate
01:15:04
something exceptional that has happened all right so the last type of error that we want to talk about is called semantic errors and semantic errors are related to the meaning of the program so if you have a semantic error in your program it will it will actually run without generating error messages but it will not do what you expected it to do it would actually
01:15:24
do something else and in particular it will do what you told it to do for example if you're translating the following expression which is x divided by two multiplied by pi notice that x here is in the denominator and the two pi is in the denominator if i want to translate that
01:15:42
into julia you might write x followed by the division symbol and then followed by two the asterisk and the symbol pi now this is not correct because multiplication and division if you remember from the first lecture have the same precedence and are evaluated from left to right
01:16:02
so when julia reads this expression what it's going to do is that it's going to say x divided by two because the division and multiplication has the same precedence and then it's going to divide the result by pi in other words it's going to say or it's going to multiply the
01:16:20
result by pi in other words it's going to say x divided by two multiplied by pi so a good way to avoid this or debug this expression is to add parentheses to make the order of the valuation that you want explicit so what you want is that you want to divide x by two pi so you put the two multiplied by pi between parentheses and then you have x divided by
01:16:43
whatever you have between the two parentheses so in some ways semantic errors are the hardest debug and that's because the interpreter or the interface that you're using whether it's a notebook or a raffle provides no information about what is going wrong only you know what
01:17:03
the program is supposed to do all right so just to summarize the flow of execution is the order in which statements are executed and we can control this flow of execution using while loop for loops and if else and else f statements the while loop and for loops
01:17:26
statements are used to evaluate certain expression usually with some variation repeatedly until some statement or some condition is satisfied in a while loop the termination condition is based
01:17:41
on a boolean expression whereas in a for loop this termination condition is based on an iterable sequence an iterable sequence is an ordered collection of elements that we can iterate over such as range string and vector and finally the if else and else if statements are used to
01:18:02
ensure that certain condition are met before we execute the code what we're going to talk about in the future is how we can group statements that makes our code easier to read and debug and we're going to figure out how we can eliminate repetitive code in a way that we
01:18:21
can ensure later whenever we want to make any adjustment or any change in the code we only make the change in one place that's it for this lecture and i'll see you in the next video
Recommendations
Series of 22 media