Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Making Decisions and Working With Strings
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
CS0007: Introduction to Computer Programming
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 6 Control Structures
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 4: Making Decisions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 9- 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18 Indexing Structures for Files.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11 Object, Object- Relational, and XML: Concepts, Models, Languages,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 2: Input, Processing, and Output
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 4 Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Part 1 Conditionals and Loops.
Chapter 1 Introduction. Copyright © 2003 by Pearson Education, Inc.1-2.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Methods and Data Programming with Alice and Java First Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
More on the Selection Structure
Chapter 4: Making Decisions.
JavaScript.
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Javascript Conditionals.
if-else-if Statements
Making Decisions in a Program
Topics The if Statement The if-else Statement Comparing Strings
Lesson 04 Control Structures I : Decision Making
Objectives After studying this chapter, you should be able to:
Chapter 4: Decision Structures and Boolean Logic
Three Special Structures – Case, Do While, and Do Until
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 4 Select…Case Multiple-Selection Statement & Logical Operators
Chapter 4: Decision Structures and Boolean Logic
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis Chapter 5: Decision Structures and Boolean Logic

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-2 Chapter Topics 5.1 Introduction to Decision Structures 5.2 Dual Alternative Decision Structures 5.3 Comparing Strings 5.4 Nested Decision Structures 5.5 The Case Structure 5.6 Logical Operators 5.7 Boolean Variables

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Decision Structures A decision structure allows a program to perform actions only under certain conditions Different types of decisions include –If, also called single alternative –If then else, also called dual alternative –Case structure for multiple alternative decisions

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Decision Structures The if statement –An action only occurs if the decision is true If condition Then Statement End If –A diamond symbol is used in flowcharts Figure 5-1 A simple decision structure

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Introduction to Decision Structures Relational Operators –Determines whether a specific relationship exists between two values –Used within the condition, a Boolean expression x > y x =y x<y x <=y x==y x!=y Table 5-1 Relational operators

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dual Alternative Decision Structures If then else statement –Executes one group of statements if it’s Boolean expression is true, or another group if its Boolean expression is false Figure 5-8 A dual alternative decision structure

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dual Alternative Decision Structures If condition Then statement Else statement End if If temperature < 40 Then Display “A little cold” Display “Get a coat!” Else Display “Nice weather” Display “And sunny!” End if

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparing Strings Most languages allow you to compare strings

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comparing Strings Other String Concerns –String and strings can be compared name1 == name 2 –String and string literals can be compared Month != “October” –String comparisons are generally case sensitive –You can also determine whether one string is greater than or less than another string (allows for sorting strings)

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Decision Structures Decisions are nested in order to test more than one condition If then if else example Figure 5-15 A nested decision structure

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Nested Decision Structures The if then else if statement can make nested logic simpler to write If score < 60 Then Display “Grade is F.” Else If score < 70 Then Display “Grade is D.” Else If score < 80 Then Display “Grade is C.” Else If score < 90 Then Display “Grade is B.” Else Display “Grade is A.” End If

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Case Structure The case structure lets the value of a variable or an expression determine which path of execution the program will take –Can be used as an alternative to nested decisions Figure 5-18 A case structure

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators Logical Operators are used between conditions to create complex Boolean expressions AND – Both conditions must be true OR – Either condition must be true NOT – Reverses the truth of an expression

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators AND example If temperature 12 Then Display “The temperature is in the danger zone.” End If OR example If temperature 100 Then Display “The temperature is in the danger zone.” End If NOT example If NOT (temperature > 100) Then Display “This is below the maximum temperature.” End If

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Logical Operators Range Checking Often used for range checking –When checking for a number inside a range, use AND If x >=20 AND x <=40 Then Display “The value is in the acceptable range.” End If –When checking for a number outside a range, use OR If x 40 Then Display “The value is outside the acceptable range.” End If

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Boolean Variables A variable of the Boolean data type can hold one or two values: true or false Declare Boolean isLunchTime If time >=12 then Set isLunchTime = True Else Set isLunchTime = False End If