 Hello, welcome back. In the last segment, we did a detailed example, a somewhat complete program using if statements. Now we are going to discuss the switch statement and logical data. So first the switch statement. So this is a little bit complicated and so you will have to bear with me as I describe it. So I will start by describing the general form. So it looks like this, which followed by an expression. Then there is the keyword case and then there is constant 1. So here you are expected to actually have a number like 5, 7, 1, 2, 3 whatever it is. And then you are going to have a group of statements. Usually these statements will end with a break. Then there is a case, another constant, another group of statements and so on. And then there will be the keyword default and there will be another group of statements which we will call the default group of statements. So that is what this switch statement looks like in general. And the expressions and the constants are required to be integers. How does this execute? So first the expression is evaluated. So when after the previous statement is executed, when we want to execute the switch statement, first the expression is evaluated. Now this value is compared with constant 1, constant 2 and so on. If some constant i is found equal, say maybe constant 2 is found equal and constant 1 is not equal. So the first constant that is found equal, let us say that is constant i or in this case constant 2. Then all statements starting with group i are executed. So from here everything is executed. If a break statement is found, so we said that it usually ends with a break. If a break statement is found, then we deem that this switch statement execution is over and the control goes on to the next statement following the switch. If there is no break statement, then it executes the next group of statements and the next group of statements and so on until a break statement appears or you execute the last statement in whatever group. So that is what the switch statement looks like. If no constant i is found equal to this expression, then the default group of statements is executed. So this statement looks like it is designed for our turtle controller. Remember what was happening in the turtle controller? We were reading in a command and command is a character but you know that a character is actually an integer, a character is a numeric type. So a character only when you print it gets printed behaves like a character but for calculation purposes it is actually an integer. So this is the perfectly fine numeric type as we required it to be. So we wanted this switch command, we wanted an integer to come out of this and the command being a character, its value is in fact an integer. If the value of this integer is equal to f, the asque value of f, then forward 100 will be executed. But after forward 100 is executed because there is a break, then the control will go on to the next statement following this. So it will go on to the next part, next repeat. If command was r then this will be executed, if command was l then this will be executed. And if none of these were present then the default this will be executed. So notice that this looks quite nice. On the other hand there is a slight problem, what is the problem? You may forget to write the break, so go back here. It is very likely that you may just forget it, if you just forget say this break what happens you execute this and you also execute this, so that is not such a good idea. So this statement is actually, I will actually suggest that you do not use the statement instead just you use the analysis. So the statement looks elegant but because of the way it is designed because of this break part it is actually a little bit dangerous to use because you may forget. And you may say that look sometimes it might be necessary to execute several groups and the book indeed gives you an example just for completeness but really my recommendation is do not use the switch statement. And in fact in this course I will not be asking you anything about the switch statement. I just described this because for the reasons of completeness if you read real code you should not get a code written by somebody else you should not get surprised when you see this switch statement. So the next topic I want to cover in this segment is logical data. So we have seen that we can evaluate conditions and we can also combine together conditions. So we can write something like if x greater than 5 and y less than 6. So we are really performing we are really doing operations on results of conditions as well. So when you might ask why should we not allow storing the results true or false of such computations. So we evaluated a condition it came out to be true and false. So let us say I want to remember that that condition came out to be true or false. So can I remember it? Can I put it in some variable? Indeed I can. So C++ has a data type bool into which values of conditions can be stored. Bool is named after the scientist George Bool who formalized the manipulation of conditions and logical data. In fact he invented something called a Boolean algebra. So we are not going to see Boolean algebra as such but we will discuss this bool data type next. So here is how we can declare a bool type of data. So I can write bool high income or low income. Of course these are going to be standard identifiers with the usual rules for identifiers. I can assign to this and I can write something like high income equals and I can put down a condition over here. So this condition will get evaluated and depending upon the value of the condition either a true or a false will go in over here. I can also write something like declare a Boolean variable fun and simultaneously initialize it to true if I want. So true is a constant or a keyword. So the first statement will set high income to true if the variable income contains value larger than 800,000 and the second statement will just set fun to true and true and false are Boolean constants and what is the use of these variables? Well Boolean variables can be used wherever conditions are expected. So I can write something like if high income tax equal to so and so. Of course before this it is expected that I will have set high income to true or false by writing a statement of this kind. So now I want to give you an exercise which is write a program to test if a given number n is prime. So we are going to do this and let us see how we would do it. So first of all you would do this, so we would ask look how do you do this manually? So there is an algorithm that you may or may not remember or may or may not know called the sieve of Eratosthenes. So how does that work? Well very quickly we start with 2 and we keep on checking whether a certain number divides n. Now this we make it a little bit more efficient by saying look if we want to check whether a given number i divides n we first have to check whether it is a prime itself or not. So that is Eratosthenes sieve, we are not going to follow that method so it does not matter if you did not understand what Eratosthenes sieve is. We are going to do something slightly different, we are going to do something which is slightly less efficient but it will do things without requiring us to remember too many things. Here is what we will do, we will start with 2 and go all the way till n minus 1 and check if any of these numbers divides n. If one of these numbers divides n then clearly n is composite. If none of these numbers divides n then n is a prime. So that is going to be our algorithm. So start with 2 as a candidate divisor and check if it divides n otherwise go on to the next candidate until n minus 1 and check if any of those numbers divides, candidate divisors divides n. And if we find any such number we will be able to conclude that in fact that number is composite otherwise that number will be we can say is prime. So let us write the program, so we will include simple CPP and we will have the main program and n is the number that we want to decide whether it is prime or composite and divisor is our candidate divisor which we are going to start off at 2. So the first thing we do is we read into read n. So we expect the user to type in whatever number he or she wants to check whether it is prime or not. Then we are going to have a Boolean variable. Remember we said that we are going to go over the candidate divisor starting at 2 and this is our first candidate divisor over here. So I should have perhaps used a bigger name candidate divisor but that is what I mean. So this is our candidate divisor and we are going to start with this and go on till n minus 1. And if ever I find that candidate divisor divides n then I will make divisor found to be true. So far I have not found any divisor for n. So I will make it false, I will set it to be false. So we will check if divisor divides n as it varies from 2 to n minus 1 and then thereby set divisor found. So if the divisor divides n then set divisor found equal to true. And how do we do this? Well we have to check for all numbers between 2 and n minus 1. So there are n minus 2 such numbers. So we are going to have a repeat loop of n minus 2 steps, n minus 2 iterations. And we are going to check if n mod divisor is 0 or in other words is n perfectly divided by divisor. If it is then we have found a divisor and so we will set divisor found to be true. And then we want to check the next divisor and therefore we will set divisor equal to divisor plus 1 and that is it. After this if divisor found is true then we know that the number is composite. So we will say if not divisor found then see out prime. We could have written if divisor found then print composite but we just wrote it in a different order else we are going to print that this number is composite that is it. So what has happened over here? We have used divisor found a Boolean variable to accumulate whether or not we have found a divisor as we go through the loop. So this is one use we can put Boolean variables to. So we are remembering the previous whether or not divisors were found previously and that is a Boolean value whether a divisor was found is either true or false so that information can be accumulated can be put in Boolean variable. And so we have given it an appropriate name and inside that we are putting this information and at the end if Boolean if divisor found is false that is we did not find any divisor at all then we are going to print out prime otherwise we will print out composite. So I would like you to execute this program mentally for n equal to 100 and I would like you to verify that it produces the correct answer. But I would also like you to think about whether you think that the program is a good program or whether it is perhaps doing too much work and if it is and you will indeed see that it is doing a necessary work we will find ways of remaining that a little bit later in the course. So what have we discussed? We discussed the switch statement and we recommended that it should not be used. We discussed logical data and we said that it can be used to remember results of conditional evaluation. In the next brief segment we are just going to summarize this lecture sequence. But before that let us take a short break. Welcome back. In the last segment we discussed the switch statement and logical data. In this segment we are nearly going to summarize the lecture. So first of all I should observe that conditional execution makes life interesting. We noted at the beginning that the tax calculation program cannot be solved, cannot be written without something like conditional execution, something like an if statement. Then we discussed three forms of if. The plain if, if, condition consequent, if then else, if condition consequent, else alternate and then the most general with several consequence and a single alternate. And I should point out that you can nest if statements inside each other. So the consequent can be a block and that block can contain if statements. But there are some pitfalls in this and those pitfalls are discussed in the book. So please read the book. We also looked at compound conditions. So conditions which are made by saying look I want this condition to be true and this condition to be true or maybe this condition to be true or this condition to be true or this condition should be false. Then we also talked about logical data. We did talk about the switch statement but I am not mentioning it over here because I really do not want you to use it. And before I finish this lecture as usual I will be, I will ask you to try the exercise not at the end of the book but at the end of the chapter. So please do that, that is absolutely necessary and I hope you will like those exercises. So that is it, that is it for this lecture sequence. Thank you.