Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators

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

Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
 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.
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.
Problem Solving and Control Statements. Using Exit to Terminate Repetition Statements There are many forms of the Exit statement, designed to terminate.
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.
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.
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.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
 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.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
Problem Solving and Control Statements: Part 2
Introduction to Computer Programming
Chapter 4 The If…Then Statement
Chapter 5- Control Structures: Part 2
Chapter 4 C Program Control Part II
Multiple selections Logical Operators Precedences
Control Statements: Part 2
Chapter 4 MATLAB Programming
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 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 5 – Control Structures: Part 2
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Control Structures – Selection
More Selections BIS1523 – Lecture 9.
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
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
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 © 1992-2011 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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

The above program Use the Select Case Statement to determine the grade © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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: © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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. Nothing is displayed © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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

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. Select Case grade Case Is < 0 Equivalent If grade < 0 Then © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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, Cases can be used to test String values © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais

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

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 <= 10, total > 1000 and number <> -1. Each selection and repetition statement evaluated only one condition with one of the operators >, <, >=, <=, = and <>. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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 True True If both are True then the condition of the If Statement is met © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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) Is the If Condition met? age >= 65 gender = "F" True False © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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. © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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: Can be written as If Not (value = 0) Then If value <> 0 Then © 1992-2011 by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais

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

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

If Not( x * y < 0 ) And ( x ^ 2 >= 100 Mod 4 ) Example #2 If Not( x * y < 0 ) And ( x ^ 2 >= 100 Mod 4 ) 1 3 4 2 5 6 7 © 1992-2011 by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon Al-Duwais