Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spf Chapter5 Conditional Logics

991 views

Published on

> Condition and operands
> if .. else if … else
> switch

Published in: Education
  • Be the first to comment

Spf Chapter5 Conditional Logics

  1. 1. Chapter 5<br />C# .NET: ConditionalLogic<br />is condition true?<br />false<br />true<br />Processing1<br />Processing2<br />
  2. 2. Condition logic / Decisions and Branching<br />Recall the problems we solved in week #1 & 2: we used “if (n1 > n2) { … }” to check condition and branch<br />This chapter will cover condition logic in detail<br />
  3. 3. General view<br />if (condition)<br />{ // condition is true <br /> // processing1<br />} <br />else <br />{ // condition is false<br /> // processing2<br />}<br />is condition true?<br />false<br />true<br />Processing1<br />Processing2<br />
  4. 4. Condition<br />if (condition)<br />{<br /> // processing1<br />} <br />else <br />{<br /> // processing2<br />}<br />- The result must be ‘true’ or ‘false’<br /><ul><li> Cannot use ‘0’ or ‘1’
  5. 5. Eg: For the condition (n1 > n2)</li></ul> => Is n1 greater than n2? <br /> The result is a ‘true’ or ‘false’<br />
  6. 6. Condition<br />if (condition)<br />{<br /> // processing1<br />} <br />else <br />{<br /> // processing2<br />}<br />How to set condition?<br />Use operand to compare<br />Eg (n1 == n2)<br /> (n1 != n2)<br />
  7. 7. Condition<br />if (condition)<br />{<br /> // processing1<br />} <br />else <br />{<br /> // processing2<br />}<br />With more than 1 condition<br />Use && for “and”:<br />(condition1) && (condition2)<br />Use || for “or”:<br />(condition1) || (condition2)<br />Eg (n1 >= 0) && (n2 >=0)<br /> => both n1 and n2 must be positive<br />Eg (n1 >= 0) || (n2 >=0)<br /> => either n1 or n2 is positive<br />
  8. 8. condition<br />Comparing Boolean type:<br />boolbuttonClicked = false;<br />if (buttonClicked == true) { … }<br />if ( !buttonClicked) { … } // not buttonClicked<br />
  9. 9. condition<br />Comparing Char type:<br />char answer = 'N';<br />if (answer == 'Y') { … }<br />
  10. 10. condition<br />Comparing String type:<br />string input = ""; // empty string<br />if (input == "exit") { … }<br />
  11. 11. condition<br />Comparing String type:<br />string input = ""; // empty string<br />if (input == "exit") { … }<br />String class has methods to do more complex checking such as Compare(), CompareTo(), StartsWith(), EndsWith(), Equals(), etc. Will cover some in Chapter 9<br /><ul><li>Good tutorial on C# string compare: </li></ul> http://alturl.com/tbwi<br />
  12. 12. Other branching – if only<br />if (condition)<br />{ // condition is true <br /> // processing1<br />} <br />// No else branch<br />is condition true?<br />false<br />true<br />Processing1<br />
  13. 13. Other branching – if .. Else if .. else<br />is condition1 true?<br />if (condition1)<br />{ // condition is true <br /> // processing1<br />} <br />else if (condition2)<br />{ // condition1 is false and <br /> // condition2 is true<br /> // processing2<br />}<br />else<br />{// condition1 is false and <br />//condition2 is false<br />// processing3<br />}<br />false<br />true<br />is condition2 true?<br />Processing1<br />false<br />true<br />Processing2<br />Processing3<br />
  14. 14. Other branching - switch<br />switch (n1)<br />{<br /> case 1:<br /> // processing1<br /> break;<br /> case 2:<br /> // processing2<br /> break;<br /> default:<br /> // processing3<br /> break;<br />}<br />n1<br />1<br />2<br />3<br />Processing2<br />Processing3<br />Processing1<br />
  15. 15. Other branching - switch<br />switch (n1)<br />{<br /> case 1:<br /> case 2:<br /> // processing2<br /> break;<br /> default:<br /> // processing3<br /> break;<br />}<br />n1<br />1, 2<br />3<br />Processing2<br />Processing3<br />
  16. 16. Other branching - switch<br />switch (n1)<br />{<br /> case 1:<br /> // processing1<br />goto case 2;<br /> case 2:<br /> // processing2<br /> break;<br /> default:<br /> // processing3<br /> break;<br />}<br />n1<br />1<br />2<br />3<br />Processing1<br />Processing2<br />Processing3<br />
  17. 17. Exercise 5.1<br />Textbook from page 93 – 98:<br />Part 1 if Statements <br />Part 2 else ... if in C# .NET <br />
  18. 18. Exercise 5.2<br />Textbook from page 98 – 106:<br />Part 3 switch Statements in C# .NET <br />Part 4 C# Operators <br />
  19. 19. Exercise 5.3(a)<br />For the GUI created in Exercise 3.2:<br />Rename all the controls to follow proper naming convention: egtextboxOutput, buttonZero, etc<br />Use winword to draft the pseudo codes for all the button click methods (you may need some variables outside the methods to be accessible by all methods)<br />
  20. 20. Exercise 5.3(b)<br />For the GUI created in Exercise 3.2:<br />Create a new project: Exercise53B<br />Select all the controls in Exercise32 and paste into this new project<br />Code and debug manually by viewing the codes <br />No worry: we shall use debugging tool to debug this project in chapter 6: Debugging<br />
  21. 21. Summary<br />Condition and operands<br />if .. else if … else<br />switch<br />

×