1 / 54

Chapter 3 . Conditional Logic In C# .NET

Chapter 3 . Conditional Logic In C# .NET. C# Tutorial by M. Abdollahi (Isfahan University of Technology – IT Center) Barbod Engineering Co. Oct. 2011 http://www.abdollahi.us. Agenda.

zuriel
Download Presentation

Chapter 3 . Conditional Logic In C# .NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 . Conditional Logic In C# .NET C# Tutorial by M. Abdollahi(Isfahan University of Technology – IT Center)Barbod Engineering Co. Oct. 2011 http://www.abdollahi.us

  2. Agenda Conditional Logic is all about the IF word. In fact, it's practically impossible to program effectively without using IF. You can write simple programs like our calculator. But for anything more complicated, you need to get the hang of Conditional Logic. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  3. Conditional Logic in C# As an example, take the calculator program you have just written. It only has a Plus button. We'll be adding another button soon, a Subtract button. Now, you can't say beforehand which of the two buttons your users will click. Do they want to add, or subtract? You need to be able to write code that does the following: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  4. Conditional Logic in C# IF the Plus button was clicked, add upIF the Minus button was clicked, subtract You can rearrange the two statements above. Was the Plus button clicked? Yes, or No?Was the Minus button clicked? Yes, or No? So the answer for each is either going to be Yes, or No - the button is either clicked, or not clicked. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  5. if Statements To test for YES or NO values, you can use an IF statement. You set them up like this: if ( ){} C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  6. if Statements So you start with the word if (in lowercase), and type a pair of round brackets. In between the round brackets, your type what you want to check for (Was the button clicked?). After the round brackets, it's convenient (but not strictly necessary) to add a pair of curly brackets. In between your curly brackets, you type your code. Your code is what you want to happen IF the answer to your question was YES, or IF the answer was NO. Here's a coding example: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  7. if Statements boolbuttonClicked = true; if (buttonClicked == true){MessageBox.Show("The button was clicked"); } Notice the first line of code: boolbuttonClicked = true; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  8. if Statements This is a variable type you haven't met before - bool. The bool is short for Boolean. You use a Boolean variable type when you want to check for true or false values (YES, or NO, if you prefer). This type of variable can only ever be true or false. The name of the bool variable above is buttonClicked. We've set the value to true. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  9. if Statements The next few lines are our IF Statement: if (buttonClicked == true) {MessageBox.Show("The button was clicked"); } C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  10. if Statements The double equals sign (==) is something else you need to get used to when using IF Statements. It means "Has a value of". The double equals sign is known as a Conditional Operator. (There are a few others that you'll meet shortly.) But the whole of the line reads: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  11. if Statements "IFbuttonClicked has a value of true“ If you miss out one of the equals signs, you'd have this: if (buttonClicked = true) What you're doing here is assigning a value of true to the variable buttonClicked. It's not checking if buttonClicked "Has a value of" true. The difference is important, and will cause you lots of problems if you get it wrong! C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  12. if Statements In between the curly brackets of the IF statement, we have a simple MessageBox line. But this line will only get executed IF buttonClicked has a value of true. Let's try it out. Start a new project for this (File > New Project). Add a button to your new form, and set the Text property to "IF Statement". Double click the button, and add the code from above. So your coding window will look like this: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  13. if Statements Run your program again, and click the button. What happens? Nothing! The reason that nothing happens is that our IF Statement is checking for a value of true: if (buttonClicked == true) C# will only execute the code between the curly brackets IF, and only IF, buttonClicked has a value of true. Since you changed the value to false, it doesn't bother with the MessageBox in between the curly brackets, but moves on instead. Run your program and click the button. You should see the message box. Now halt the program and change this line: boolbuttonClicked = true; to this boolbuttonClicked = false; So the only change is from true to false. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  14. else Statement You can also say what should happen if the answer was false. All you need to do is make use of the else word. You do it like this: if (buttonClicked = = true){ }else{ } C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  15. else Statement So you just type the word else after the curly brackets of the IF Statement. And then add another pair of curly brackets. You then write your code for what should happen if the IF Statement was false. Change your code to this: So the whole thing reads: "IF it's true that buttonClicked has a value of true, do one thing. If it's not true, do another thing.“ Run your program, and click the button. You should see the second MessageBox display. Halt the program and change the first line back to true. So this: boolbuttonClicked = true; instead of this: boolbuttonClicked = false; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  16. else Statement Run the program again, and click the button. This time, the first message box will display. The whole point of using IF ..Else Statements, though, is to execute one piece of code instead of some other piece of code. You can also extend the IF statement and add an else ... if part. This will be useful in our calculator program. Click below to continue the lessons. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  17. Else ... If in C# .NET Instead of using just the else word, you can use else if, instead. If we use our calculator as an example, we'd want to do this: So the code checks to see which button was clicked. If it's the Plus Button, then the first IF Statement gets executed. If it's the Minus Button, then the second IF Statement gets executed. But else if is just the same as if, but with the word else at the start. In fact, we can now add a minus button to our calculator. We'll use else if. boolplusButtonClicked = true;boolminusButtonClicked = false; if (plusButtonClicked == true){//WRITE CODE TO ADD UP HERE}elseif (minusButtonClicked == true){//WRITE CODE TO SUBTRACT HERE} C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  18. Else ... If in C# .NET You should then see a section headed Recent Projects: So open up your calculator project again. To do this, click the link on the Start Page Tab in Visual C#. If you can't see your Start Page tab, click its icon at the top of the C# software: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  19. Else ... If in C# .NET Look for your calculator project here. You can also click File > Recent Projects from the menu bar at the top. If both of those fail, click File > Open Project. Navigate to where you saved your project. Open up the file that ends in .sln. With your calculator project open, add a new button. Set the following properties for it in the Properties Window: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  20. Else ... If in C# .NET Name: btnMinusFont: Microsoft Sans Serif, 16, BoldLocation: Move it to the right of your Plus buttonSize: 49, 40Text: - Now double click your Minus button to get at its code. Add the following two Boolean variables outside of the Minus button code, just above it: boolplusButtonClicked = true;boolminusButtonClicked = false; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  21. Else ... If in C# .NET You coding window will then look like this: Your coding window will then look like the one below: Now add the following code inside of the Minus button: total1 = total1 + double.Parse(txtDisplay.Text);txtDisplay.Clear( ); plusButtonClicked = false;minusButtonClicked = true; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  22. Else ... If in C# .NET All we've done here is to set up two Boolean variables. We've set them both to false outside of the code. (They have been set up outside of the code because other buttons need to be able to use them; they have been set to false because no button has been clicked yet.) When the Minus button is clicked, we'll set the Boolean variable minusButtonClickedto true and the plusButtonClicked to false. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  23. Else ... If in C# .NET But the first two lines are exactly the same as for the Plus button: total1 = total1 + double.Parse(txtDisplay.Text);txtDisplay.Clear( ); The first line just moves the numbers from the text box into the total1 variable. The second line clears the text box. Now access the code for your Plus button. Add two lines of code to the end: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  24. Else ... If in C# .NET So the only thing you are adding is this: plusButtonClicked = true;minusButtonClicked = false; The Plus button resets the Boolean variables. This time, plusButtonClicked gets set to true, and minusButtonClicked gets set to false. It was the other way round for the Minus button. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  25. Else ... If in C# .NET The reason we're resetting these Booleans variables is because we can use them in an if else statement. We can add up if the plusButtonClicked variable is true, and subtract if minusButtonClickedis true. We'll still do the calculating in the Equals button. So change your equals button to this: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  26. Else ... If in C# .NET We're using Conditional Logic to decide which of the two buttons was clicked. The first IF statement checks if the plusButtonClickedvariable is true. If it is, then the addition gets done (this is exactly the same as before). If the first IF Statement is false, then C# moves down to the else if statement. If minusButtonClickedis true, then the subtraction gets done instead. The only difference between the addition and subtraction lines is the Operator symbols: a plus (+) instead of a minus (-). C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  27. Else ... If in C# .NET The final two lines of code are the same as before - convert the number to text and display it in the text box, and then reset the total1 variable to zero. Run your calculator and try it out. You should be able to add and subtract! C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  28. Else ... If in C# .NET Exercise Finish your calculator by adding Divide and Multiply buttons to your form. Write the code to make your calculator Divide and Multiply. For this exercise, you're just adding two more Boolean variables to your code. You can then add more else if statements below the ones you already have. You'll also need to add two more lines to the code for your four Operator buttons. These two lines need to reset the Boolean variables to either true or false. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  29. Else ... If in C# .NET For example, here's the code for the Minus button to get your started: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  30. Else ... If in C# .NET So we now have four Boolean variables outside the button code, one for the plus button, one for minus button, one for divide button, and one for the multiply button. When you click a button, its Boolean variable gets set to true. Do the same for the other three buttons. Then write your else if statements. This is quite a tricky exercise, though. Probably your hardest so far! In the next lesson, we'll look at something called Switch Statements. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  31. Switch Statements in C# .NET An easier way to code the calculator is by using a switch statement instead of an else if statement. A switch statement allows you to check which of more than one option is true. It's like a list of if statements. The structure of a switch statement looks like this: After the word switch, you type a pair of round brackets. In between the round brackets, you type what you want to check for. You are usually testing what is inside of a variable. Then type a pair of curly brackets. In between the curly brackets, you have one case for each possible thing that your variable can contain. You then type the code that you want to execute, if that particular case is true. After your code, type the word break. This enables C# to break out of the Switch Statement altogether. Switch(param) { case “value1” : //Do First Job break; case “value2” : //Do Second Job break; ….. default : //Do Default Job } C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  32. Switch Statements in C# .NET We'll use our calculator as a coding example. Our four buttons set a Boolean variable to either true or false. Instead of doing this, we could have the buttons put a symbol into a string variable. Like this: So the last line of code puts the + symbol into a string variable we've called theOperator. It will only do this if the button is clicked. The other buttons can do the same. We can then use a switch statement in our Equals button to check what is in the variable we've called theOperator. This will tell us which button was clicked. Here's the code that would go in the Equals button. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  33. Switch Statements in C# .NET Switch(theOperator) { case “+” : //ADD UP HERE break; case “-” : //SUBTRACT HERE break; case “*” : //MULTIPLY HERE break; case “/” : //DIVIDE HERE break; default : //DEFAULT CODE HERE break; } In between the round brackets after the word switch, we've typed the name of our variable (theOperator). We want to check what is inside of this variable. It will be one of four options: +, -, *, /. So after the first case, we type a plus symbol. It's in between double quotes because it's text. You end a case line with a colon: case "+" : C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  34. Switch Statements in C# .NET The code to add up goes on a new line. After the code, the break word is used. So what you're saying is: "If it's the case that theOperator holds a + symbol, then execute some code" We have three more case parts to the switch statement, one for each of the math symbols. Notice the addition of this, though: default ://DEFAULT CODE HEREbreak; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  35. Switch Statements in C# .NET You use default instead case just "in case" none of the options you've thought of are what is inside of your variable. You do this so that your program won't crash! Switch statements can be quite tricky to get the hang of. But they are very useful if you want to test a variable for a list of possible things that could be in the variable. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  36. Switch Statements in C# .NET Exercise Adapt the equals button on your calculator so that it uses a switch statement instead of if .. else if Statements. You can get rid of all your Boolean variables. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  37. C# Operators You've already met one Conditional Operator, the double equals sign ( == ). You use this in IF Statement when you want to check if a variable "has a value of" something: if ( myVariable == 10) {//EXECUTE SOME CODE HERE} So the above line reads, "IF whatever is inside of myVariable has a value of 10, execute some code." Other Conditional Operators you'll use when you're coding are these: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  38. C# Operators C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  39. C# Operators Because you need to learn these Operators, let's get some practice with them. Start a new project. Add two text boxes and a button to your form. Resize the text boxes and type 8 as the Text property for the first text box, and 7 as the Text property for the second text box. Set the Text property for the button to the word "Compare". Your form will then look like this: Double click the button to get at the coding window. What we'll do is to get the numbers from the text boxes and test and compare them. So the first thing to do is to set up some variables: intfirstNumber;intsecondNumber; C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  40. C# Operators Then get the text from the text boxes and store them in the variables (after converting them to integers first.) firstNumber = int.Parse(textBox1.Text);secondNumber = int.Parse(textBox2.Text); What we want to do now is to compare the two numbers. Is the first number bigger than the second number? To answer this, we can use an IF Statement, along with one of our new Conditional Operators. So add this to your code: if (firstNumber>secondNumber){MessageBox.Show("The first number was greater than the second number");} C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  41. C# Operators Your coding window will then look like this (our message box above is only on two lines because it can't all fit on this page): So in between the round brackets after if, we have our two variables. We're then comparing the two and checking to see if one is Greater Than ( > ) the other. If firstNumberis Greater Than secondNumberthen the message box will display. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  42. C# Operators Run your program and click your button. You should see the message box display. Type a 6 in the first text box, and click the button again. The message box won't display. It won't display because 6 is not greater than 7. The message box code is inside of the curly brackets of the IF Statement. And the IF Statement only gets executed if firstNumber is Greater Than secondNumber. If it's not, C# will just move on to the next line. You haven't got any more lines, so C# is finished. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  43. C# Operators Stop your program and go back to your code. Add a new if statement below your first one: if (firstNumber<secondNumber){MessageBox.Show("The first number was less than thesecond number");} Again, our message box above is spread over two lines because there's not enough room for it on this page. Your message box should go on one line. But the code is just about the same! The thing we've changed is to use the Less Than symbol ( < ) instead of the Greater Than symbol ( > ). We've also changed the text that the message box displays. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  44. C# Operators Run your program, and type a 6 in the first text box. You should see your new message box display. Now type an 8 in the first text box, and click your button. The first message box will display. Can you see why? If your program doesn't work at all, make sure it is like ours in the image below: C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  45. C# Operators With your program still running, type a 7 in the first box. You will then have a 7 in both text boxes. Before you click your button, can you guess what will happen? The reason that nothing happens at all is because you haven't written any code to say what should happen if both numbers are equal. For that, try these new symbols: >=(Greater Than or Equal to) And these ones <= (Less Than or Equal to) C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  46. C# Operators Try these new Conditional Operators in place of the ones you already have. Change the text for your message boxes to suit. Run your code again. When you click the button, both message boxes will display, one after the other. Can you see why this happens? Another Conditional Operator to try is Not Equal To ( != ). This is an exclamation mark followed by an equals sign. It is used like this: if (firstNumber!=secondNumber ){//SOME CODE HERE} C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  47. C# Operators So, "IF firstNumber is not equal to secondNumber execute some code.“ You can even use the exclamation mark by itself. You do this when you want to test for a false value between the round brackets after if. It's mostly used with Boolean values. Here's an example: booltestValue = false; if (!testValue){MessageBox.Show("Value was false");} C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  48. C# Operators So the exclamation mark goes before the Boolean value you want to test. It is a shorthand way of saying "If the Boolean value is false". You can write the line like this instead: if (testValue== false) But experienced programmers just use the exclamation mark instead. It's called the NOT Operator. Or the "IF NOT true" Operator. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  49. C# Operators Try not to worry if you don't have a thorough grasp of all the Conditional Operators yet - you'll get the hang of them as you go along. But try the next exercise. Exercise Write a small program with a text box and a button. Add a label to ask people to enter their age. Use Conditional Logic to test how old they are. Display the following messages, depending on how old they are: Less than 16: "You're still a youngster."Over 16 but under 25: "Fame beckons!"Over 25 but under 40: "There's still time."Over 40: "Oh dear, you've probably missed it!" C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

  50. C# Operators Only one message box should display, when you click the button. Here's some code to get you started: int age; age = int.Parse(textBox1.Text); if (age < 17){MessageBox.Show("Still a youngster.");} For the others, just add more IF Statements, and more Condition Operators. C# Tutorial by M. Abdollahi - Chapter 3 . Conditional Logic in C# - http://www.abdollahi.us

More Related