# Chapter 7

Lecture Set 5 Control Structures Part A - Decisions Structures Objectives Slide 2 You have seen almost all of this before Use the Boolean data type in decision

statements Use if statements and switch-case statements to make decisions Use logical operators to create complex conditional expressions Learn how to write and evaluate conditional expressions (sometimes called Boolean expressions because they have true/false values) 2/27/20 03:42 AM Introduction to Decision Statements -1 Programming involves the use of three basic control structures -

Recursion aside, these are the only three constructs you need to build algorithms Slide 3 The sequence structure The decision structure The repetition structure This is it -- End of story! 2/27/20 03:42 AM Introduction to Decision Statements -2 This lecture set discusses the decision statements provided by C#

Statements execute conditionally based on the outcome of a decision (condition) Three kinds of decision-making or alternative path execution structures Slide 4 Single alternative decision statements ( ifthen) Double alternative statements (if-thenelse) Multiple alternative statements (including the switch-case)

Much like Java, VB, and C++ . . . there is not much new under the sun2/27/20 03:42 AM Introduction to Boolean Data The Boolean data type has two values (rather like an on/off switch) Many properties are of the Boolean data type (can store only Boolean values) Slide 5

true signifies on false signifies off btnExit.Visible lblPlayAgain.Enabled 2/27/20 03:42 AM Declaring a Boolean Variable Declare a Boolean variable Uninitialized Boolean variables have a value of false Boolean valid; Declare and initialize a Boolean variable

Boolean browseMode = true; Declare multiple Boolean variables Boolean test1, test2; Slide 6 2/27/20 03:42 AM Boolean Assignment Statements The keywords true and false are used in Boolean assignment statements Boolean expressions may also be used Example:

Boolean valid = valid = valid = Slide 7 valid; true; false; x < y && y < z; 2/27/20 03:42 AM Decision-making Statements

Slide 8 Applications need the capability to execute one group of statements in certain circumstances and other statements in other circumstances These statements are called decision statements or decision structures or alternative statements The if statement is used to make choices as to what steps in our program are executed next 2/27/20 03:42 AM Decisions (Pseudocode) If the input date is greater than the current date then Display a message box indicating the input is invalid. End of If statement

Slide 9 2/27/20 03:42 AM Flowchart of a Decision Statement Slide 10 2/27/20 03:42 AM If Statements and Comparison Operators Slide 11 An if statement executes one group of

statements when a condition is true and another group of statements (or no statements at all) when a condition is false Comparison operators are used in conditional statements Conditional (comparison) operations always produce a Boolean result 2/27/20 03:42 AM Comparison Operators Slide 12

Equal to (==) Not equal to (!=) Less than (<) Greater than (>) Less than or equal to (<=) Greater than or equal to (>=) 2/27/20 03:42 AM Logical Operators Name Description && Returns true if both expressions are true. This operator evaluates the second expression only if necessary || Returns true if either expression is true. This operator evaluates the second expression only if necessary ! &

| Slide 13 Reverses the value of a Boolean expression Returns true if both expressions are true. This operator always evaluates both expressions Returns true if either expression is true. This operator always evaluates both expressions 2/27/20 03:42 AM Using Comparison Operators (Example) Boolean result; int value1 = 3; int value2 = 5; result = value1 < value2; // true

result = value1 + 2 < value2 1; // false Slide 14 2/27/20 03:42 AM Using Comparison Operators (Example, continued) Parentheses can clarify the order of evaluation The following two statements are equivalent: result = value1 + 2 < value2 1;

result = (value1 + 2) < (value2 1); Slide 15 2/27/20 03:42 AM Evaluating a Condition Slide 16 2/27/20 03:42 AM One-Way if Statement (Syntax) aka Single Alternative Decision Statement if (condition) { statements // a block of one or more statements } // end if // next sequential statement // (NSS)

Slide 17 condition must evaluate to a Boolean value If the condition is true, the statements execute If the condition is false, the statements do not execute Execution continues at the statement following the 2/27/20 03:42 AM } // end if (the NSS) One-Way if Statement (Example) Boolean currentValue; . . .

if (currentValue == true) { // Statements that execute when currentValue is true } // end if // next sequential statement Slide 18 2/27/20 03:42 AM One-Way (Single Alternative) if Statement Slide 19 2/27/20 03:42 AM Comparison Operations with Dates

Yep! We are a bit ahead of ourselves. But you will get a taste of using the DateTime type Comparison operations can be performed on dates Dates in the past are less than dates in the future Example: DateTime startDate = #3/22/2007#; DateTime endDate = #3/24/2007#; if (startDate < endDate) == true) { endDate = System.DateTime.Today; } // end If

Slide 20 2/27/20 03:42 AM Comparison Operations with Numeric Data Types Comparison operations can be performed on numeric data Example: int value1 = 90; if (value1 < 100) { // Statements execute when // value1 is less than 100 } // end if // next sequential statements

Slide 21 2/27/20 03:42 AM Comparison Operations with Strings Comparison operations can be performed with strings Strings are compared character-by-character from left to right String comparisons are often performed in two ways

Slide 22 Case sensitive (binary comparison) as in C# and VB A < B < E < Z < a < b < e < z Case insensitive (text comparison) as in VB only (A=a) < (B=b) < (E=e) < (Z=z) 2/27/20 03:42 AM Introduction to Two-way if Statements aka Double alternative decision structures

(if-then-else) One statement block executes when a condition is true and another statement block executes when the condition is false if (condition) {Then statements} else {Else statements} Slide 23 2/27/20 03:42 AM Two-way if Statements (Syntax) if (condition) { // statements(true) } else {

// statements(false) } // end if // next sequential statements Slide 24 Statements(true) execute if the condition is true Statements(false) execute if the condition is false 2/27/20 03:42 AM Double Alternative if Statements (Example) If Grade is greater than 75, set Pass to

true. Otherwise, set Pass to false Boolean pass; int grade = 80; if (grade > 75) pass = true; else pass = false; } // end if // next sequential statements Slide 25 2/27/20 03:42 AM Double-Alternative if Statement Slide 26 2/27/20 03:42 AM Introduction to Multiway if

Statements Slide 27 aka Multiple Alternative Decision Structures Multiway if statements have three or more possible outcomes 2/27/20 03:42 AM Multiway If Statements (Syntax) if (condition1) { [then statements] } else if (condition2) {

[else if Statements1] {additional else ifs and statements} } [else] } [else Statements] } // end if // next sequential statements Slide 28 2/27/20 03:42 AM Multiway if Statements condition1 is first tested If true, then the first statement block executes

Execution continues at the statement following the decision-making statement If false, condition2 is tested and then the remaining conditions are tested If no conditions are true, then the statements in the else block execute Slide 29 (Dissection)

The else block is optional If it is omitted and no conditions are true the effect is as if the multiway if was 2/27/20 03:42 AM skipped Multiway If Statement Slide 30 2/27/20 03:42 AM Multiway If Statement (Example) int numericGrade = 84; char letterGrade; if (numericGrade >= 90) letterGrade = A;

if statements can be written in different ways The Code Editor automatically indents blocks in an if statement if statements can be nested Slide 32 Choose the if statement that is most readable Good idea to always use braces for a statement block even though the block consists of one statement you never know when you will add a

second statement to the block (and forget to add the {} ) One if statement can contain another if statement 2/27/20 03:42 AM Introduction to Switch-Case Statements Slide 33 switch-case statements are similar to multiway if statements The same form of expression must be

used in each condition switch-case statements are faster than comparable multiway if statements switch-case statements tend to be more readable than comparable multiway if statements 2/27/20 03:42 AM Switch Case Statement (Syntax) switch (testExpression) { case constantExpression-1: statement-block1 break; . . . [case constantExpression-n: statement-blockn break;] [default:

statements break;] } // end switch // next sequential statements Slide 34 2/27/20 03:42 AM switch-case Statement testExpression is evaluated once Each constantExpression is then tested. If true, the corresponding statement-block executes and the switch-case statement ends (control continues with the NSI) Each constantExpression is tested in order

When an constantExpression is found to be true, the corresponding statement block executes and the switch-case statement ends If no constantExpression is true, then the statements in the default block execute Slide 35 (Dissection) If there is no default block it is as though the switch-case was skipped 2/27/20 03:42 AM switch-case Statement

(Example) int quarter = 1; string quarterString; switch (quarter) case 1: quarterString = "First; break; case 2: quarterString = "Second; break; case 3: quarterString = "Third; break; case 4: quarterString = "Fourth; break; default: quarterString = "Error; break; } //end switch

// next sequential statements Slide 36 2/27/20 03:42 AM Switch-case Statement (slightly modified from original version) Slide 37 2/27/20 03:42 AM switch-case Statement (Variations) A list of case values can be created using a list of cases with no associated statements:

case 1: case 3: case 5: // statement block The statement block is executed when the testExpression has any one of the values 1, 3, or 5. Slide 38 2/27/20 03:42 AM Logical Operators (Introduction) Slide 39

Logical operators are used in conjunction with comparison and arithmetic operators Logical operators perform the same task as a conjunction (and) or a disjunction (or) in English The logical operators are && || ! & | 2/27/20 03:42 AM Logical Operators (Precedence) Logical operators (Not, And, Or) fit into the overall order of precedence scheme

Arithmetic operators are evaluated first Comparison operators are evaluated second Logical operators are evaluated last (of all the BINARY operators), from left to right in the following order: Slide 40 ! & | (not) or && or || (and)

(or) 2/27/20 03:42 AM Logical Operators (Example) Evaluation of an expression (be sure you know how to do these): Boolean result; result = (3 + 4) > 6 && (4 + 1) < 6; result = 7 > 6 && 5 < 6; result = true && true; result = true; If I remove the parentheses in line 1, does it Why? How? Slide 41

change the result? 2/27/20 03:42 AM

## Recently Viewed Presentations

• Medical Terminology Lesson 2
• You are free to attend whichever lecture you prefer Section 1 is taught by Nathan Friedman in ENGTR 0100 Section 2 is taught by Yi Lin in ENGTR 1080 Computing Facilities The Faculty of Engineering computers have all the software...
• Monday 8/25. Come in, find your seat. They are for the most part in Alphabetical Order. Write down your homework: Signed Syllabus, Facebook, Class Dojo- due Tomorrow; Supplies due Friday.
• If the Book Fits, Read It! Finding a Good Fit Book By Barbara A. Smith Carolina Forest Elementary Grade 2 The Daily 5 * * * * * * * * Would you ever wear a pair of shoes that...
• Venous filling: In a warm room, the veins of a normal foot are dilated and full of blood, even when the patient is lying horizontally. In an ischaemic foot, the veins collapse and sink below the skin surface to look...
• Entrepreneurship Chapter 5 Developing the Right Marketing Mix * Marketing Mix = the 4 Ps Product Price Place Promotion Goal: Bring the right product to the right place at the right price with the right promotion.
• This slide shows all the books of the Bible arranged in order, by section, on a bookshelf. It is a visual depiction to help get across the idea that the Bible is not just a book, but a library of...
• EDA - Strong Cities, Strong Communities Economic Visioning Challenge. EDA - Regional Innovation Strategies Program. Census - CitySDK (Software Development Kit) Department of Transportation. Smart City Challenge. Ladders of Opportunity Initiative. Transportation Investment Generating Economic Recovery (TIGER) grants