Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Programming Logic and Design Sixth Edition
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Making Decisions
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
1 2.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 8 Dental Payment Application Introducing CheckBox es and Message Dialogs.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
 2009 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Security Panel Application Introducing the Select Case Multiple-Selection Statement.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #5 Control Statements: Part 2.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Control Statements: Part1  if, if…else, switch 1.
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
 2007 Pearson Education, Inc. All rights reserved Control Statements: Part2.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Introduction to Computer Programming
Chapter 5- Control Structures: Part 2
Chapter 4 C Program Control Part II
Multiple selections Logical Operators Precedences
Control Statements: Part 2
Control Structures: Part 2
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 3: Introduction to Problem Solving and Control Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 6 Control Statements: Part 2
Problem Solving and Control Statements
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Using C++ Arithmetic Operators and Control Structures
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Lecture 9: Implementing Complex Logic
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al- Duwais

Select … Case Multiple-Selection Statement If an algorithm takes different actions based on series of decisions to test variable values, Select…Case multiple- In slide#3, Select … Case statement is used to determine whether each grade is the equivalent of an A, B, C, D or F. selection statement can be used. Select Case controlling expression Case CaseLabel1 Action1 Case CaseLabel2 Action2 Case Else ElseAction End Select The output of the program is displayed in slide#5. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 2

3 The above program Use the Select Case Statement to determine the grade

© by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al- Duwais 4 The above program Use If Then ElseIf Statement to determine the grade - This program is equivalent to the one in previous slide#3 that uses Select Case

© by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al- Duwais 5 When The user enters grade 77 and press the Calculate Score button of the Select Case Statement (The code in Slide#3) When The user enters grade 77 and press the Calculate Score button of the If Then Else Statement (The code in Slide#4) When The user Press one of the Calculate Score Button Without Entering any score in the TextBox When The user enters grade 89 and press one of the Calculate Score button

Select … Case Multiple-Selection Statement A grade in the range: – 100 represents a Perfect A – 90–99 represents an A, – 80–89 represents a B, – 70–79 represents a C, – 60–69 represents a D – and 0–59 represents an F. We assume that the user enters a grade in the range 0–100. The expression following the keywords Select Case (in this case, grade ) is called the controlling expression. It is compared in order with each Case. If a matching Case is found, the code in the Case executes, then program control proceeds to the first statement after the Select … Case statement. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 6

Select … Case Multiple-Selection Statement A Case statement can specify multiple actions: – Example: The Following Case has two actions: – The output will be as follows: Keyword To specifies the range; – Example: Case 80 To 89 Means all the values [80,81,82,….,87,88,89] ranging between 80 to 89 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 7

Select … Case Multiple-Selection Statement If no match occurs between the controlling expression’s value (grade) and a Case label, the optional Case Else executes. It is not necessary to write the Case Else. – Example: In Slide#3: If the User enters the value 59, the action of the Case Else will be executed as follows: © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 8

Select … Case Multiple-Selection Statement If no match occurs and the Select … Case does not contain a Case Else, program control simply continues with the first statement after the Select … Case. – Example: If the code in slide#3 does not have the Case Else as bellow, when the user Enters the number 59 nothing will happen. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 9 Nothing is displayed

Select … Case Multiple-Selection Statement Case Else commonly is used to deal with invalid values. In this example, we assume all values entered by the user are in the range 0–100. When used, the Case Else must be the last Case. The required End Select keywords terminate the Select … Case statement. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al- Duwais 10

© by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al- Duwais 11

Select … Case Multiple-Selection Statement Types of Case Statements – Case statements also can use relational operators to determine whether the controlling expression satisfies a condition. For example: uses keyword Is along with the relational operator, <, to test for values less than 0. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 12 Select Case grade Case Is < 0 If grade < 0 Then Equivalent

Select … Case Multiple-Selection Statement Types of Case Statements, – Multiple values can be tested in a Case statement by separating the values with commas (, ) as in, – Case 0, 5 To 9 which tests for the value 0 or values in the range 5–9. – Also, Case s can be used to test String values © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 13

Select … Case Multiple-Selection Statement Select … Case Statement to test String values – Example: (The Select Color Example) A program that asks the user to enter one of the three basic colors (red,green,blue) if the input is correct the label change its color to the entered color. © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 14

Select … Case Multiple-Selection Statement (The Select Color Example Program) © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 15

Logical Operators A condition is an expression that results in a Boolean value—True or False. So far, we’ve studied only simple conditions, such as count 1000 and number <> -1. Each selection and repetition statement evaluated only one condition with one of the operators >, =,. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 16

Logical Operators To handle multiple conditions more efficiently, the logical operators can be used to form complex conditions by combining simple ones. Logical operators are And, Or, Xor and Not. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 17

Logical Operators Logical And Operator – Suppose we wish to ensure that two conditions are both True in a program before a certain path of execution is chosen. – In such a case, we can use the logical And operator as follows: – If gender = "F" And age >= 65 Then seniorFemales += 1 End If – This If … Then statement contains two simple conditions. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 18

Logical Operators The condition gender = "F" determines whether a person is female and the condition age >= 65 determines whether a person is a senior citizen. The two simple conditions are evaluated first, because the precedence of = and >= are both higher than the precedence of And. If gender = "F" And age >= 65 © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais True If both are True then the condition of the If Statement is met 19

Logical Operators When this combined condition is True, the seniorFemales count is incremented by 1. The readability of the preceding combined condition can be improved by adding parentheses: (gender = "F") And (age >= 65) © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais Is the If Condition met? age >= 65 gender = "F" True False True FalseTrueFalse 20

Logical Operators Logical Or Operator – Now let’s consider the Or operator. – Suppose we wish to ensure that either or both of two conditions are True before we choose a certain path of execution. – We use the Or operator as in the following program segment: If (semesterAverage >= 90 Or finalExam >= 90 ) Then resultLabel.Text = "Student grade is A" End If – This statement also contains two simple conditions. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 21

Logical Operators The condition semesterAverage >= 90 is evaluated to determine whether the student deserves an “A” in the course because of his performance throughout the semester. The condition finalExam >= 90 is evaluated to determine whether the student deserves an “A” in the course because of his performance on the final exam. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 22

Logical Operators The If … Then statement then considers the combined condition (semesterAverage >= 90 Or finalExam >= 90 ) and awards the student an “A” if either or both of the conditions are True. The text "Student grade is A" is displayed, unless both of the conditions are False. The And operator has a higher precedence than Or. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 23

© by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al- Duwais 24

Logical Operators Logical Xor Operator – A condition containing the logical exclusive OR (Xor) operator is True if and only if one of its operands results in a True value and the other results in a False value. – If both operands are True or both are False, the entire condition is False. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 25

Logical Operators Logical Not Operator – The Not (logical negation) operator enables you to “reverse” the meaning of a condition. – Unlike the logical operators And, Or and Xor, which each combine two conditions, the logical negation operator is a unary operator, requiring only one operand. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 26

Logical Operators The logical negation operator is demonstrated by the following program segment: If Not (value = 0) Then resultLabel.Text = "The value is " & value End If The parentheses around the condition value = 0 are necessary because the logical negation operator ( Not ) has a higher precedence than the equality operator. © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais 27

Logical Operators In most cases, you can avoid using logical negation by expressing the condition differently with relational or equality operators. This flexibility helps you express conditions more naturally. For example, the preceding statement can be written as follows: © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais If Not (value = 0) ThenIf value <> 0 Then Can be written as 28

Operators’ Precedence © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais 29

Example #1 X += ( 4 \ y * 2 ) ^ ( x – 2 ) © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

Example #2 If Not( x * y = 100 Mod 4 ) © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais