© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Making Decisions and Working With Strings
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
CS0007: Introduction to Computer Programming
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.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
The if Statement The code in methods executes sequentially.
Making Decisions and Working With Strings
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual C++ Programming: Concepts and Projects
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
CSCI Chapter 4 Making Decisions and Working with Strings Instructor: Bindra Shrestha University of Houston – Clear Lake.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 4 Making Decisions and Working With Strings.
CIS 115 Lecture 7 Selection / Decisions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Chapter 4 The If…Then Statement
Decision Structures and Boolean Logic
Chapter 3 Making Decisions
Chapter 4 Controlling the Flow Adapted From: Starting Out with Visual Basic 2012 (Pearson)
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter Making Decisions and Working With Strings 4.
Chapter Making Decisions 4. Relational Operators 4.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.
InputBox. Format of the InputBox Function Prompt - message to the user Title - text for the box's title bar Default - default text for user's input Xpos.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Chapter 4 Select … Case Multiple-Selection Statement & Logical Operators 1 © by Pearson Education, Inc. All Rights Reserved. -Edited By Maysoon.
Computer Science Up Down Controls, Decisions and Random Numbers.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.
Chapter 4 The If…Then Statement
Making Decisions and Working With Strings
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Lecture 3- Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Selection Statements.
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
The System.exit() Method
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Decision Structures and Boolean Logic
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Presentation transcript:

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 1 Unit 4 Decisions Chapter 4 Making Decisions and Working with Strings

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 2 Chapter 4 Topics This chapter covers the Visual Basic.NET decision statements  If … Then  ElseIf It also discusses the use of  Radio Buttons  Message Boxes

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 3 The Decision Structure The Decision Structure Allows a Program to Have More Than One Path of Execution

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 4 Order of Statement Execution Thus far, all of our code statements have been executed sequentially To write meaningful programs our code needs to be able to  Execute code conditionally (this chapter)  Repeat sections of code (next chapter)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 5 The Decision Structure Flowchart of a typical decision structure Evaluate the condition Execute, or not some code Condition Conditional Code True False

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 6 The If…Then Statement The If…Then Statement Causes Other Statements to Execute Only Under a Certain Condition

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 7 If…Then Statement Syntax New keywords used above:  If  Then  End If condition Then statement (more statements as needed) End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 8 Relational Operators Usually the condition is formed with a relational operator  >Greater than  <Less than  =Equal to  <>Not equal to  >=Greater than or equal to  <=Less than or equal to

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 9 Binary Operators Operators are classified as to how many operands each takes Relational operators are binary operators -- each has two operands, e.g.  length > width  size <= 10 Relational operators yield a True or False result

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 10 If…Then Examples If sales > Then getsBonus = True End If If sales > Then getsBonus = True commissionRate = 0.12 daysOff = daysOff + 1 End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 11 If…Then Rules The 'If' and the 'Then' must be on the same line Only a remark may follow the 'Then' The 'End If' must be on a separate line Only a remark may follow the 'End If'

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 12 If…Then Conventions The code between the 'If…Then' and the 'End If' is indented Visual Basic.NET does not require this It is a convention among programmers to aid in the readability of programs By default, the Visual Basic.NET editor will automatically do this indentation as you enter your program

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 13 Relational Operators with Math Operators Either or both of the relational operator operands may themselves be expressions Math operators are done before the relational operators If x + y > a - b Then lblMessage.Text = "It is true!" End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 14 Relational Operators with Function Calls Either or both of the relational operator operands may themselves be function calls If Val(txtInput.Text) < 100 Then lblMessage.Text = "It is true!" End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 15 Boolean Variables as Flags A flag is a Boolean variable that signals when some condition exists in the program They can be used as the condition If quotaMet Then lblMessage.Text = "You have met your sales quota" End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 16 What an 'If…Then' Really Tests For Visual Basic.NET first evaluates the conditional expression If the result is 0, the condition is regarded as being False If the result is anything else, the condition is regarded as being True

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 17 The If…Then…Else Statement The If...Then...Else Statement Executes One Group of Statements If the Condition Is True and Another Group of Statements If the Condition Is False

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 18 'If…Then' vs. 'If…Then…Else' The 'If…Then' construct will execute or not a group of statements (do something or do nothing) The 'If…Then…Else' construct will execute one group of statements or another group (do this or do that)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 19 The 'If…Then…Else' Structure Condition Execute If True True False Execute If False

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 20 'If…Then…Else' Example If temperature < 40 Then lblMesage.Text = “A little cold, isn’t it?” Else lblMesage.Text = “Nice weather we’re having!” End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 21 The If…Then…ElseIf Statement The If...Then…Elseif Statement Is Like a Chain of If...Then...Else Statements They Perform Their Tests, One After the Other, Until One of Them Is Found to Be True

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 22 Two Mutually Exclusive Choices ('If…Then…Else') The 'If…Then…Else' statement has two choices The conditional statement will either be True or False Hence, exactly one of the two choices will be selected They are mutually exclusive

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 23 More Than Two Mutually Exclusive Choices The 'If…Then…ElseIf' statement provides a series of mutually exclusive choices In pseudo code: If it is very cold Then Wear a coat Elseif it is chilly Wear a light jacket Elseif it is windy Wear a windbreaker Elesif it is hot Wear no jacket

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 24 In Visual Basic.NET Syntax If condition 1 Then Statement(s) 1 Elseif condition 2 Then Statements(s) 2 Elseif condition 3 Then Statements 3 … End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 25 In Flowchart Form C1C1 C2C2 C3C3 Statement(s) 1 True Statement(s) 2 True Statement(s) 3 True False

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 26 Example of Usage If average < 60 Then lblGrade.Text = "F" ElseIf average < 70 Then lblGrade.Text = "D" ElseIf average < 80 Then lblGrade.Text = "C" ElseIf average < 90 Then lblGrade.Text = "B" ElseIf average <= 100 Then lblGrade.Text = "A" End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 27 Contrast the Preceding Code To This Code Without the Elses If average < 60 Then lblGrade.Text = "F" End If If average < 70 Then lblGrade.Text = "D" End If If average < 80 Then lblGrade.Text = "C" End If If average < 90 Then lblGrade.Text = "B" End If If average <= 100 Then lblGrade.Text = "A" End If

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 28 The (Optional) Trailing Else A sequence of ElseIfs may end with a plain else (called a trailing Else) If none of the conditions were True, the statement(s) after this Else will be executed Continuing with the preceding example on the next slide:

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 4: Chapter 4: Slide 29 Use of a Trailing Else If average < 60 Then lblGrade.Text = "F" ElseIf average < 70 Then lblGrade.Text = "D" ElseIf average < 80 Then lblGrade.Text = "C" ElseIf average < 90 Then lblGrade.Text = "B" ElseIf average <= 100 Then lblGrade.Text = "A" Else lblGrade.Text = "Invalid" End If