20
CHAPTER 5 C# .NET: Conditional Logic Processin g1 Processin g2 is condition true? tru e fals e

Spf Chapter5 Conditional Logics

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Spf Chapter5 Conditional Logics

CHAPTER 5C# .NET: Conditional Logic

Processing1

Processing2

is condition true?

true

false

Page 2: Spf Chapter5 Conditional Logics

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

Page 3: Spf Chapter5 Conditional Logics

GENERAL VIEW

if (condition){ // condition is true // processing1} else { // condition is false // processing2}

Processing1

Processing2

is condition true?

true

false

Page 4: Spf Chapter5 Conditional Logics

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’

Page 5: Spf Chapter5 Conditional Logics

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)

Page 6: Spf Chapter5 Conditional Logics

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

Page 7: Spf Chapter5 Conditional Logics

CONDITION

Comparing Boolean type: bool buttonClicked = false; if (buttonClicked == true) { … } if ( !buttonClicked) { … } // not

buttonClicked

Page 8: Spf Chapter5 Conditional Logics

CONDITION

Comparing Char type: char answer = 'N'; if (answer == 'Y') { … }

Page 9: Spf Chapter5 Conditional Logics

CONDITION

Comparing String type: string input = ""; // empty string if (input == "exit") { … }

Page 10: Spf Chapter5 Conditional Logics

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

Page 11: Spf Chapter5 Conditional Logics

OTHER BRANCHING – IF ONLY

if (condition){ // condition is true // processing1} // No else branch

Processing1

is condition true?

true

false

Page 12: Spf Chapter5 Conditional Logics

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?

Page 13: Spf Chapter5 Conditional Logics

OTHER BRANCHING - SWITCH

switch (n1){ case 1: // processing1 break; case 2: // processing2 break; default: // processing3 break;}

Processing1

1

Processing2

Processing3

2 3

n1

Page 14: Spf Chapter5 Conditional Logics

OTHER BRANCHING - SWITCH

switch (n1){ case 1: case 2: // processing2 break; default: // processing3 break;}

Processing2

Processing3

1, 2 3

n1

Page 15: Spf Chapter5 Conditional Logics

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

Page 16: Spf Chapter5 Conditional Logics

EXERCISE 5.1

Textbook from page 93 – 98: Part 1 if Statements Part 2 else ... if in C# .NET

Page 17: Spf Chapter5 Conditional Logics

EXERCISE 5.2

Textbook from page 98 – 106: Part 3 switch Statements in C# .NET Part 4 C# Operators

Page 18: Spf Chapter5 Conditional Logics

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)

Page 19: Spf Chapter5 Conditional Logics

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

Page 20: Spf Chapter5 Conditional Logics

SUMMARY

Condition and operands if .. else if … else switch