Upload
hock-leng-puah
View
504
Download
2
Embed Size (px)
DESCRIPTION
> Condition and operands > if .. else if … else > switch
Citation preview
CHAPTER 5C# .NET: Conditional Logic
Processing1
Processing2
is condition true?
true
false
CONDITION LOGIC / DECISIONS AND BRANCHING
Recall the problems we solved in week #1 & 2: we used “if (n1 > n2) { … }” to check condition and branch
This chapter will cover condition logic in detail
GENERAL VIEW
if (condition){ // condition is true // processing1} else { // condition is false // processing2}
Processing1
Processing2
is condition true?
true
false
CONDITION
if (condition){ // processing1} else { // processing2}
- The result must be ‘true’ or ‘false’- Cannot use ‘0’ or ‘1’
- Eg: For the condition (n1 > n2) => Is n1 greater than n2? The result is a ‘true’ or ‘false’
CONDITION
if (condition){ // processing1} else { // processing2}
How to set condition?Use operand to compare
Eg (n1 == n2) (n1 != n2)
Operand
Description
== is the same as
!= is NOT the same as
> is greater than
< is smaller than
>= is greater than or equal to
<= is smaller than or equal to
! Not (use for boolean type only)
CONDITION
if (condition){ // processing1} else { // processing2}
With more than 1 conditionUse && for “and”:(condition1) && (condition2)
Use || for “or”:(condition1) || (condition2)
Eg (n1 >= 0) && (n2 >=0) => both n1 and n2 must be positive
Eg (n1 >= 0) || (n2 >=0) => either n1 or n2 is positive
CONDITION
Comparing Boolean type: bool buttonClicked = false; if (buttonClicked == true) { … } if ( !buttonClicked) { … } // not
buttonClicked
CONDITION
Comparing Char type: char answer = 'N'; if (answer == 'Y') { … }
CONDITION
Comparing String type: string input = ""; // empty string if (input == "exit") { … }
CONDITION Comparing String type:
string input = ""; // empty string if (input == "exit") { … }
String class has methods to do more complex checking such as Compare(), CompareTo(), StartsWith(), EndsWith(), Equals(), etc. Will cover some in Chapter 9
Good tutorial on C# string compare: http://alturl.com/tbwi
OTHER BRANCHING – IF ONLY
if (condition){ // condition is true // processing1} // No else branch
Processing1
is condition true?
true
false
OTHER BRANCHING – IF .. ELSE IF .. ELSE
if (condition1){ // condition is true // processing1} else if (condition2){ // condition1 is false and // condition2 is true // processing2}else{// condition1 is false and //condition2 is false // processing3}
Processing1
is condition1 true?
true
false
Processing2
Processing3
true
false
is condition2 true?
OTHER BRANCHING - SWITCH
switch (n1){ case 1: // processing1 break; case 2: // processing2 break; default: // processing3 break;}
Processing1
1
Processing2
Processing3
2 3
n1
OTHER BRANCHING - SWITCH
switch (n1){ case 1: case 2: // processing2 break; default: // processing3 break;}
Processing2
Processing3
1, 2 3
n1
OTHER BRANCHING - SWITCH
switch (n1){ case 1: // processing1 goto case 2; case 2: // processing2 break; default: // processing3 break;}
Processing1
1
Processing2
Processing3
2 3
n1
EXERCISE 5.1
Textbook from page 93 – 98: Part 1 if Statements Part 2 else ... if in C# .NET
EXERCISE 5.2
Textbook from page 98 – 106: Part 3 switch Statements in C# .NET Part 4 C# Operators
EXERCISE 5.3(A)
For the GUI created in Exercise 3.2: Rename all the controls to
follow proper naming convention: eg textboxOutput, buttonZero, etc
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)
EXERCISE 5.3(B)
For the GUI created in Exercise 3.2: Create a new project:
Exercise53B Select all the controls in
Exercise32 and paste into this new project
Code and debug manually by viewing the codes
No worry: we shall use debugging tool to debug this project in chapter 6: Debugging
SUMMARY
Condition and operands if .. else if … else switch