Flow Control: boolean expressions, if selec5on statements (Alice In Ac5on, Ch 4)

Similar documents
Methods (part 1) Alice In Action, Ch 2

Methods (part 2) Alice In Action, Ch 2

Events (Alice In Ac.on, Ch 6)

Methods (part b) Alice In Ac5on, Ch 2

Events (Alice In Action, Ch 6)

Getting Started with Java Using Alice. 1 Copyright 2013, Oracle and/or its affiliates. All rights reserved.

5. Selection: If and Switch Controls

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Alice In Action With Java (Introduction To Programming) By Joel Adams READ ONLINE

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Decision Structures. Chapter 4

Creating a Fancier Fox and Inheritance in Alice 3

Repetition everywhere comparing while in a method and as an event. Comparison

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Alice 3 Tricks in Mini Trivia (1/4) By Vicki Zhang Duke University Under the direction of Professor Susan Rodger June 2017

Week Lesson Assignment SD Technology Standards. Knowledge Check. Project Project Project Power Point 3.1. Power Point 3.

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Slicing. Slice multiple parts 13,0601,1489,1664(SP6P1)

Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

CompSci 94 Making Decisions February 8, Prof. Susan Rodger

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Flow Control. CSC215 Lecture

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

logical operators and else-if statements

PREPARING FOR PRELIM 1

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

REFERENCE MATERIALS. Assignment, Display, and Input Evaluates expression and assigns the result to the variable a.

Duration 30 minutes Notes This tutorial was built using Alice

Lesson 27 CPF Weather Station

Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Lecture 4 8/24/18. Expressing Procedural Knowledge. Procedural Knowledge. What are we going to cover today? Computational Constructs

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

Scopes Global and Block

Programming Language Concepts, cs2104 Lecture 04 ( )

Class-level Methods. Chapter 6 part 1

Checking for Collision. by Vicki Zhang Under the direction of Professor Susan Rodger Duke University July 2017

Princess & Dragon Part 1: Objects in an Alice World

CS 106A, Lecture 7 Parameters and Return

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Name Section: M/W T/TH Number Definition Matching (6 Points)

CS 106 Introduction to Computer Science I

Chapter 4 The If Then Statement

CSCI 1061U Programming Workshop 2. C++ Basics

Computational Expression

Control Structures in Java if-else and switch

Chapter 2. Section 2.3 if and if-else statements. CS 50 Hathairat Rattanasook

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture Notes on Loop Optimizations

Python Programming: An Introduction to Computer Science

Software Development Techniques. December Sample Examination Paper. Time: 3 hours

Decision Making in C

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

BIL101E: Introduction to Computers and Information systems Lecture 8

C311 Lab #3 Representation Independence: Representation Independent Interpreters

Lesson 25 Flood Warning System

4: Specifying State-based Behavior With UML Statechart Diagrams

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Lecture 4. Conditionals and Boolean Expressions

Programming Logic and Design Sixth Edition

CS1 Lecture 5 Jan. 26, 2018

20/08/14. Computer Systems 1. Instruction Processing: FETCH. Instruction Processing: DECODE

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Session 3: JavaScript - Structured Programming

Software Testing Fundamentals. Software Testing Techniques. Information Flow in Testing. Testing Objectives

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

3 Programming. Jalal Kawash Mandatory: Chapter 5 Section 5.5 Jalal s resources: How to movies and example programs available at:

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Copyright 2008 CS655 System Modeling and Analysis. Korea Advanced Institute of Science and Technology

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

boolean & if-then-else

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Consolidated High School District Orland Park, Illinois Windows Movie Maker Instruction Guide

CS 221 Lecture. Tuesday, 13 September 2011

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Static Program Analysis

Assessment - Unit 3 lessons 16-21

Introduction to Mail Merge. Use IT+

AP Computer Science Principles Exam Reference Sheet

Programming for Experimental Research. Flow Control

Python Boot Camp. Day 3

Midterms Save the Dates!

Fundamentals of Programming CS-110. Lecture 2

Repetition. Add in Objects. Position Objects. Monkey Eat Bananas Repetition Simple Loops and Conditional Loops for Alice 3

Monkey Eat Bananas Repetition Simple Loops and Conditional Loops for Alice 3

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

Plotting Points. By Francine Wolfe Professor Susan Rodger Duke University June 2010

Alice. Coverage. Mathematical Expressions, Conditional Statements, Control Structures. Arithmetic Expressions Built-in Functions Conditional Execution

Introduction to PowerPoint 2007

What You ll Learn Today

CHAPTER 5 FLOW OF CONTROL

CS 540: Introduction to Artificial Intelligence

CS 221 Lecture. Tuesday, 11 October 2011

BASICS OF GRAPHICAL APPS

Mail Merge. To Use Mail Merge: Selecting Step by Step Mail Merge Wizard. Step 1:

Transcription:

CS 101 Lecture 21 Flow Control: boolean expressions, if selec5on statements (Alice In Ac5on, Ch 4) 8 November 2012 Slides Credit: Joel Adams, Alice in Action 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 2 1

Flow Control Control statements introduced in today if: directs program flow along one of two paths 3 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 4 2

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 5 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 6 3

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() 7 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 8 4

Relational Operators (continued) 9 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 10 5

Boolean Operators (continued) 11 Boolean Operators (continued) var1 var2 var1 && var2 var1 var2!var1 true true true true false true false false true false false true false true true false false false false true This is called a truth table. 12 6

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 13 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 14 7

if Statement Mechanics (continued) if statement behavior is also called selective flow or selection. 15 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. 16 8

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 17 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 18 9

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 19 Building if Statement Conditions (continued) 20 10

Building if Statement Conditions (continued) 21 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 22 11

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. http://en.wikipedia.org/wiki/uss_yorktown_(cg-48) Question: How do we prevent this? 23 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 24 12

Validating Parameter Values (continued) 25 Validating Parameter Values (continued) 26 13

Validating Parameter Values (continued) 27 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 28 14

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: &&,,! 29 15