C-SHARP - 3.9 Condition (Demo)
We now find ourselves on Visual Studio in order to put into practice the notion of condition in C#. We will therefore take the example we saw in the previous video of a young man who goes to the cinema alone and must be at least 18 years old to access it. First, we need a variable that will store the age of this young man. We will therefore declare an int type variable that we will call age and we will assign the value 19 to it. Then we must create the condition that will allow us to check whether the age of the person is greater than or equal to 18. big that he is of legal age To do this in C#, you have to do an If If in French means if. Then you put parentheses and this is where you will enter your condition, the condition is that the age is greater than or equal to 18. Then we open braces and it is inside that we will put our instructions Here we will refer to the user that if he is an adult then you can enter. So we're going to do a Console.WriteLine() and inside we're going to write "You're of legal age, you can enter the cinema." So here to summarize, we created a condition from the if to which we placed a condition. And inside this conditional block, we place an instruction. We are going to launch the program And you see that the program shows us that you are of legal age, you can enter the cinema because the value Is greater than or equal to 18. that which is contained in the age variable. Now if we change the value of the age variable and put 12, what will happen? I launch and you see? Nothing is displayed. Why ? Well in reality here we test if our age is greater than or equal to 18. and only if the result of this test is true then we execute the instruction made between braces. You are going to tell me but we want our program to be complete, to deal with any situation whether you are a minor or an adult. I'm coming ! In C# and in all programming languages, there is what is called the "else" which means otherwise in French This "else" is completed with the if and it will process all the cases that the if does not process . When the compiler goes to run your program, it will first test whether the condition is true or false. If the condition is true then the else is ignored. On the other hand, if the condition is false then the else will intervene to process all the other cases. Here in this situation the role of else will be to deal with the case where the young man is a minor. Last thing the else does not need a condition because it handles all the other cases that the if does not handle. So between the braces of the else, we will put a Console.WriteLine() which will display "You are a minor, you must be accompanied by an adult." Now if we launch the program, the age is equivalent to 12 we first test if the age is greater than or equal to 18, it is false so we go directly to else and we execute the instruction that is between its braces or display: "You are a minor, you must be accompanied by an adult." Now that we have seen the if and the else, we also have the possibility of adding another condition by doing an else if, basically we have specified a condition in the if for example age is = at 18 but that we want to add a condition for another specific case, for example when the person is 65 years old we want to display this time you are 65 years old, you benefit from a reduction. You know that the else includes all the other cases that are not in the if so if we put this instruction in the else if we are 12 years old for example, we will have this instruction which will be executed and that is not what we research. We will first change the condition of the if and put age == 18, the double equals you remember that it allows to do a test of equality if the age is equal to 18 years. Unlike simple = which allows you to assign a value. Then in the Console.WriteLine() we will put for example "Promo: -20%" for those who are 18 years old. we enter the first And if I put 4 for example we enter the case of else which processes all the rest. Small parenthesis, when you have only one statement in your conditions you can remove the braces for readability reasons, however if you have two statements in your block for example like that, it is no longer possible. This is only possible when there is only one instruction. And to finish, we will see another way to make conditions in C#. Let's go back to our cinema program, let's imagine that there is a reduction for several slices of age which will result in lots of if else if etc. So our program will not be very optimized and unreadable. However our policy as a developer is to make programs optimized and clear. We have to respect it. In order to solve this problem we have what is called the switch. It will allow us to avoid putting dozens of ifs all over the place. Now ! To make a switch is very simple! You type switch and between the parentheses you put the variable with which you want to test, here it is is the age variable so you put the age variable between brackets, then you open braces and we will now indicate all the possible cases. First, what we must do first is to indicate the general case which will take all the cases that we are not going to indicate, such as the else in quotes. To do this, we must write default: Then we put all the instructions we want here I'm just going to put a Console.WriteLine() to display the following sentence: “You don't benefit from a reduction” this treats all the cases that will not be specified and I end with a break; In the switch, we always end a case with a break, it's to tell the compiler that we're getting out of this case. If we don't put it on, it will loop and won't be able to get out of case. So it is important to put it every time at the end of each case to get out of the switch! Now let's move on to the first case which will deal with all children under 4 years old. To do that, you type case which means clause then here you put the condition so we will put less than 4 then: and there we will put our instruction. We will display: “Admission is free for children under 4 years old. " And N' remember to break it; Then below to redo another clause you make box then the condition that you want for example all those who have an age between 5 and 12 (box <= 12) etc. And we will make several stories of it to be able to do several tests. Now we are going to change the value of the age variable and you see each time we enter each clause, you will notice that this way of doing things is the best for this kind of case More optimized and much more readable. You imagine yourself doing lots of if else if everywhere in your program. Your code will look like nothing and it will be difficult to find you. Especially since over time, you may write much longer programs. So you must be rigorous and take good habits now! Anyway, that's it for this video. I hope she has you more and that everything has been clear for you. See you next time in a new video So you must be rigorous and take good habits now! Anyway, that's it for this video. I hope she has you more and that everything has been clear for you. See you next time in a new video So you must be rigorous and take good habits now! Anyway, that's it for this video. I hope she has you more and that everything has been clear for you. See you next time in a new video