Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 06 14 September.

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

1.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
Decision Structures Chapter 4. Chapter 4 Objectives To understand: o What values can be stored in a Boolean variable o What sequence structures are and.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
2-1 Making Decisions Sample assignment statements PayAmount = Hours * Rate PayAmount = 40*Rate + (Hours – 40)*Rate*1.5.
Alice in Action with Java
Alice in Action with Java
The If/Else Statement, Boolean Flags, and Menus Page 180
Alice in Action with Java
CIS101 Introduction to Computing Week 12 Spring 2004.
Calvin and Hobbes Teach Properties and Functions Created by Daniel MacDonald under the direction of Professor Susan Rodger Duke University June 2013.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Creating Functions Deborah Nelson Duke University Professor Susan Rodger July 22, 2008.
As you come in…  DOWNLOADS FOR TODAY:  CarGameTeacherStarter.a2w  Online student textbook.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
Programming Logic Program Design. Objectives Steps in program development Algorithms and Pseudocode Data Activity: Alice program.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
Chapter 2 - Algorithms and Design
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Events (2) (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping with.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Flow Control: Repetition with For Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Events (Alice In Action, Ch 6) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Java Decision Making and booleans (Java: An Eventful Approach, Ch 4), Slides Credit: Bruce, Danyluk and Murtagh CS 120 Lecture October 2012.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Alice in Action with Java Chapter 5 Lists and Arrays.
Nonvisual Arrays by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012.
Methods (part 2) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS 120 Lecture 03 4 September 2012.
31/01/ Selection If selection construct.
Flow Control: Repetition with While Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Alice in Action with Java Chapter 4 Flow Control.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
CompSci 4 Chap 6 Sec 2 Sep 30, 2010 Prof. Susan Rodger “All your troubles are due to those ‘ifs’,” declared the Wizard. If you were not a Flutterbudget.
These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.
Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture August 2012.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Alice in Action with Java Chapter 4 Flow Control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Functions (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Alice in Action with Java Chapter 2 Methods. Alice in Action with Java2 Objectives Build world-level methods to help organize a story into scenes and.
Chapter 3: Decisions and Loops
Putting Objects in Motion
Repetition Control Structures
Programming Logic Alice.
Alice in Action with Java
Microsoft Visual Basic 2005 BASICS
Making Decisions in a Program
Conditions and Ifs BIS1523 – Lecture 8.
2-1 Making Decisions Sample assignment statements
Alice in Action with Java
3. Decision Structures Rocky K. C. Chang 19 September 2018
Microsoft Visual Basic 2005: Reloaded Second Edition
Using Lists and Functions to Create Dialogue
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Life is Full of Alternatives
Selection Statements Chapter 3.
The Selection Structure
Presentation transcript:

Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012

2 Flow Control Flow: sequence of steps for performing a user story Flow control statement: structure for managing flow Flow control statements used in previous chapters –doInOrder : produces a sequential execution –doTogether : produces a parallel execution –methods and functions: name a block of statements

3 Flow Control Control statements introduced in today –if : directs program flow along one of two paths

4 Objectives Use the Boolean type and its basic operations Use Boolean variables, expressions and functions to control if statements (and later while statements) Use the if statement to perform some statements while skipping others

5 Boolean Variables Boolean values: true, false Boolean Variables: are of the Boolean type –Used to store a value of true or false –Can be used in condition for if or while statement –How to create a Boolean variable Click create new variable (or parameter ) button Specify Boolean as variable (or parameter) type

6 Boolean Functions Return a value of true or false Can act as a condition in an if or while statement Alice: many predefined Boolean functions refer to an object’s bounding box –Example: obj.isBehind(obj2) true, if obj ’s position is beyond obj2 ’s rear face false, otherwise

7 Boolean Expressions Boolean expression –Producing a value of true or false, which can be Stored in a boolean variable Returned from a boolean function Used in if or while statement, as the description of a condition, and the basis for decision making –E.g. if (temperature > 95) && (notRain == true) goToSwim()

8 Relational Operators Produce true or false values Six relational operators: ==, !=,, >= Located in functions pane of world ’s details area Most often used to compare Number values Example: hoursWorked > 40 –hoursWorked is a Number variable –true when more than 40 hours have been worked

9 Relational Operators (continued)

10 Boolean Operators Used to modify or combine relational operations Three Boolean operators: AND, OR, NOT Located in functions pane of world ’s details area Example: age > 12 && age < 20 –age is a Number variable –Teen number compared to condition returns true

11 Boolean Operators (continued)

12 Boolean Operators (continued) var1var2var1 && var2var1 || var2!var1 true false truefalse truefalse truefalsetrue false true This is called a truth table.

13 Introducing Selective Flow Control Example: a scene with a princess and a dragon –Princess meets a mute dragon and asks questions –Dragon shakes its head to respond yes or no Objective: write a shakeHead() method –Parameter: yesOrNo, a String –If yesOrNo == “yes”, dragon shakes head up and down –If yesOrNo == “no”, dragon shakes head sideways –Use an if statement to produce conditional behavior

14 if Statement Structure of an if statement: –if (Condition ) { Statements 1 } else { Statements 2 } Value of a condition determines direction of flow –If Condition is true, Statements 1 are selected –If Condition is false, Statements 2 are selected

15 if Statement Mechanics (continued) if statement behavior is also called selective flow or selection.

16 Building if Statement Conditions The if control structure is at bottom of edit area After dragging it to the edit area, you get Note that it’s ok to leave the else block as Do Nothing.

17 Building if Statement Conditions Coding the condition of the if statement –Click on the yesOrNo parameter –Drag parameter into the editing area –Drop the parameter onto the condition’s placeholder –Choose other and then type “yes”

18 Building if Statement Conditions Overview for coding the remainder of shakeHead() –Add headMovement variable for amount of turn –Add turn() statements for up and down motion –Add turn() statements for sideways motion

19 Building if Statement Conditions (continued) Building a scene method that uses shakeHead() –princess greets dragon using a say() message –princess asks four questions –shakeHead() is called in response to each question Click the Play button to test the program

20 Building if Statement Conditions (continued)

21 Building if Statement Conditions (continued)

22 The wait() Statement Pauses a program for specified number of seconds Form of wait() statement: wait(numSecs); Use of wait() scene with dragon and princess –Inserted between princess’s first and second lines

USS Yorktown On 21 September 1997, while on maneuvers off the coast of Cape Charles, Virginia, a crew member entered a zero into a database field causing a divide by zero error in the ship's Remote Data Base Manager which brought down all the machines on the network, causing the ship's propulsion system to fail. Yorktown was "dead in the water" for 2 hours and 45 minutes Question: How do we prevent this?

24 Validating Parameter Values if statement can be used to guard set of statements –Flow enters only if parameter values are valid Example: check distance value passed to jump() –Check for positive value with condition distance > 0 –Check jump length with distance < MAX_DISTANCE –Combine two conditions with the AND ( && ) operator distance > 0 && distance <= MAX_DISTANCE How to incorporate validating logic using if structures –Place original jump() logic onto true path (outer if ) –Place validating logic in the false path (nested if ) Nested logic

25 Validating Parameter Values (continued)

26 Validating Parameter Values (continued)

27 Validating Parameter Values (continued)

28 Summary Flow control statement: controls the flow of statement execution if statement: directs flow along one of two paths based on evaluation of a condition

29 Summary (continued) Condition: a Boolean entity producing a true or false value Boolean variable: holds value of true or false Boolean function: returns true or false value Boolean expression: produces true or false value –Relational operators: ==, !=,, >= –Boolean operators: &&, ||, !

Student To Do’s Readings: –Alice in Action, Chapter 4 This week’s Alice HW (Due Monday): Problem 2.5 (loopDeeLoop) Problem 3.9 (shrink the trolls) –Be sure to use an Object parameter as the target. –Your method should be able to shrink anything! –Use Object methods for both! –Comment your code (part of the HW grade). 30