Chapter 4 The If…Then Statement

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Chapter 5 The if Statement  Conditional control structure, also called a decision structure  Executes a set of statements.
Advertisements

1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Program Design and Development
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Chapter 4 The If…Then Statement
Computer Science Selection Structures.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture Set 5 Control Structures Part A - Decisions Structures.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 5: More on the Selection Structure
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working.
Chapter 4 October 22, The If Statement Programs make decisions If(condition){ Statement(s); } Condition  boolean expression Evaluates to either.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
Conditional Control Structures Chapter 5. Goals and Objectives Understand conditional control structures. Demonstrate the use of decision structures to.
CPS120: Introduction to Computer Science Decision Making in Programs.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
5.04 Apply Decision Making Structures
IE 8580 Module 4: DIY Monte Carlo Simulation
A variable is a name for a value stored in memory.
Chapter 5 The if Statement
Chapter 4 Assignment Statement
Visual Basic I Programming
Visual Basic 6 (VB6) Data Types, And Operators
UNIT 4 Lesson 13 If statements.
Chapter 4: Making Decisions.
The Selection Structure
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Making Decisions.
Chapter 4 – Control Structures Part 1
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Making Decisions in a Program
Chapter 6 Sub Procedures
Topics The if Statement The if-else Statement Comparing Strings
Chapter 5 The Do…Loop Statement
Lesson 8: Boolean Expressions and "if" Statements
Chapter 3: Introduction to Problem Solving and Control Statements
Selection Statements Chapter 4 Attaway MATLAB 4E.
3 Control Statements:.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Introduction to Problem Solving and Control Statements
Microsoft Visual Basic 2005: Reloaded Second Edition
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 3: Selection Structures: Making Decisions
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

Chapter 4 The If…Then Statement 5/19/2018 12:42 PM Chapter 4 The If…Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression For example, the statement If x = 5 Then y = 20 End If assigns the value 20 to y only if x is equal to 5. Refer to page 93 in the text. © 2010 Lawrenceville Press

Chapter 4 Relational Operators 5/19/2018 12:42 PM Chapter 4 Relational Operators Operator Meaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to Refer to page 93 in the text. Relational operators are used to form Boolean expressions. Boolean expressions evaluate to True or False. Note that the equal sign is used as both an assignment operator and a relational operator. © 2010 Lawrenceville Press

Chapter 4 The If…Then…Else Statement 5/19/2018 12:42 PM Chapter 4 The If…Then…Else Statement Contains an Else clause that is executed when the If condition evaluates to false. For example, the statement If x = 5 Then y = 20 Else y = 10 End If assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5. Refer to pages 94 and 95 in the text. © 2010 Lawrenceville Press

Chapter 4 Nested If…Then…Else Statements 5/19/2018 12:42 PM Chapter 4 Nested If…Then…Else Statements Should be indented to make the logic clear. Nested statement executed only when the branch it is in is executed. For example, the statement If x = 5 Then y = 20 Else If x > 5 Then y = 10 Else y = 0 End If End If evaluates the nested If…Then…Else only when x is not equal to 5. Refer to page 95 in the text. © 2010 Lawrenceville Press

Chapter 4 The If…Then…ElseIf Statement 5/19/2018 12:42 PM Chapter 4 The If…Then…ElseIf Statement Used to decide among three or more actions. Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement If x < 5 Then y = 20 ElseIf x < 10 Then y = 40 ElseIf x < 15 Then y = 80 End If would give very different results if the conditions were ordered differently. Refer to page 96 in the text. © 2010 Lawrenceville Press

Chapter 4 The Select…Case Statement 5/19/2018 12:42 PM Chapter 4 The Select…Case Statement The result of an expression determines which statements to execute. The Case Else code is optional and is executed when none of the previous cases are met: Select Case numLegs Case 2 Me.lblMessage.Text = "human" Case 4 Me.lblMessage.Text = "beast" Case 8 Me.lblMessage.Text = "insect" Case Else Me.lblMessage.Text = "???" End Select Refer to pages 96 and 97 in the text. © 2010 Lawrenceville Press

Chapter 4 The Select…Case Is Statement 5/19/2018 12:42 PM Chapter 4 The Select…Case Is Statement Compares the result of an expression to a range of values to determine which statements to execute. For example: Select Case score Case Is < 10 Me.lblMessage.Text = "Nice try." Case Is < 25 Me.lblMessage.Text = "Good." Case Is >= 25 Me.lblMessage.Text = "Great!" End Select Refer to page 97 in the text. © 2010 Lawrenceville Press

Chapter 4 The Rnd() Function 5/19/2018 12:42 PM Chapter 4 The Rnd() Function Uses a formula to generate a sequence of numbers that are each greater than 0 and less than 1 and then returns one number from the sequence. A random integer in a range is generated by using the formula: (highNum – lowNum + 1) * Rnd() + lowNum Random integers are produced by using the Int() function along with the Rnd() function: Int(21 * Rnd() + 10) '10 to 30 The Randomize() statement initializes the random number generator. Refer to pages 98 and 99 in the text. © 2010 Lawrenceville Press

A set of steps that outline how to solve a problem. 5/19/2018 12:42 PM Chapter 4 Algorithms A set of steps that outline how to solve a problem. Can be implemented in plain English or in a mix of English and program code called pseudocode. Algorithms allow a programmer to think through a program before actually typing code, which may reduce errors in logic. Refer to page 101 in the text. © 2010 Lawrenceville Press

Chapter 4 Static Variables 5/19/2018 12:42 PM Chapter 4 Static Variables Declared with the keyword Static. Have a lifetime the duration of the program's running time. Used to extend the lifetime of local variables in a procedure. Should be explicitly initialized when declared. A better choice than a global variable because the scope of the variable can be limited. Refer to page 102 in the text. Static variables are necessary in event procedures with variables that should be retained in memory throughout program execution. © 2010 Lawrenceville Press

Chapter 4 Compound Boolean Expressions 5/19/2018 12:42 PM Chapter 4 Compound Boolean Expressions More than one Boolean expression in a single condition. Formed using the And, Or, or Not operators. Refer to pages 104 and 105 in the text. Compound Boolean expressions use more than one Boolean expression to determine if a condition is true or false. © 2010 Lawrenceville Press

Chapter 4 And Truth Table 5/19/2018 12:42 PM Chapter 4 And Truth Table And Exp1 Exp2 Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

Or Exp1 Exp2 Result True False Chapter 4 Or Truth Table 5/19/2018 12:42 PM Chapter 4 Or Truth Table Or Exp1 Exp2 Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

Chapter 4 Not Truth Table 5/19/2018 12:42 PM Chapter 4 Not Truth Table Not Exp Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

Chapter 4 The MessageBox Class 5/19/2018 12:42 PM Chapter 4 The MessageBox Class A predefined dialog box that displays a message to the user. Includes the Show() method for displaying the dialog box. For example: MessageBox.Show(message) Refer to page 107 in the text. © 2010 Lawrenceville Press

Chapter 4 Counter Variables 5/19/2018 12:42 PM Chapter 4 Counter Variables A variable that is incremented by a constant value. Used for counting guesses, the numbers of values entered, the number of times a button was clicked, and so on. The value of a counter is updated in a statement similar to: counter = counter + 1 Should be initialized when declared and updated by an unchanging amount. Refer to page 108 in the text. Many algorithms involve counting. Applications written for algorithms that involve counting use a counter variable for storing a number that is incremented by a constant value. © 2010 Lawrenceville Press

Chapter 4 Assignment Operators 5/19/2018 12:42 PM Chapter 4 Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment Refer to page 108 in the text. © 2010 Lawrenceville Press

Chapter 4 The CheckBox Control 5/19/2018 12:42 PM Chapter 4 The CheckBox Control (Name) should begin with chk. Text is the text displayed next to the box. Checked is set to True if the box should be displayed as checked. An If…Then statement is often used to determine if a check box is checked or cleared. Refer to page 109 in the text. A Click event is sometimes coded for a check box. This procedure executes when a check box is clicked and usually includes code to determine the state of the check box and then performs actions depending on whether the check box was selected or cleared. © 2010 Lawrenceville Press

Chapter 4 Line-Continuation Character 5/19/2018 12:42 PM Chapter 4 Line-Continuation Character The underscore character is the line-continuation character. There must be a space before and nothing after and cannot be within quotation marks. Used for dividing code to make it more readable. Refer to page 110 in the text. © 2010 Lawrenceville Press