Example: Multiple Conditions Execution Control with If Else and Boolean Questions Alice.

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

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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Parameters and Event-Handler Methods Alice. Mouse input Interactive programs often allow the user to use a mouse to click buttons in a windows-based interface.
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Parameters and Event-Handler Methods Sec 50 Web Design.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
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.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Execution Control with If/Else and Boolean Functions
Execution Control with If/Else and Boolean Functions Sec 52 Web Design.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Parameters and Event-Handler Methods Sec 50 Web Design.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Parameters and Event-Handler Methods Alice. Mouse clicks Interactive programs often allow the user to mouse click an object in the display. buttons in.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Execution Control with If/Else and Boolean Functions Alice.
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
CSC1401 Using Decisions in Java - 1. Recall from Alice We only wanted to shoot a lightning bolt at a philosopher So, we used the If statement.
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Lesson thirteen Conditional Statement "if- else" ©
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
6. NS.8 APPLY AND EXTEND PREVIOUS UNDERSTANDINGS OF NUMBERS TO THE SYSTEM OF RATIONAL NUMBERS.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Parameters and Event-Handler Methods
Obj: to recognize built-in functions and expressions HW: Read Section 3.1 and do HW4 Edmodo article due Sunday at 8pm Edmodo feedback due 8pm 10/17 Do.
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Execution Control with If/Else and Boolean Functions
Chapter 3: Decisions and Loops
Programming: Simple Control Structures
IF if (condition) { Process… }
CS320n –Visual Programming
Parameters and Event-Handler Methods
Programming: Simple Control Structures
Chapter 4: Decision Structures and Boolean Logic
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Three Special Structures – Case, Do While, and Do Until
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Parameters and Event-Handler Methods
Parameters and Event-Handler Methods
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
CS2011 Introduction to Programming I Selections (I)
Chapter 4: Decision Structures and Boolean Logic
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Parameters and Event-Handler Methods
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Execution Control with If/Else and Boolean Questions Example: Multiple Conditions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Presentation transcript:

Example: Multiple Conditions Execution Control with If Else and Boolean Questions Alice

The Zeus world revisited Recall the Zeus world (Chapter 5 Section 2) Testing this world, we found two significant problems Anything the user clicked was zapped by Zeus’ bolt – not just philosophers! Philosophers could be zapped with lightning more than once!

Problem What we need in the Zeus world is conditional execution Check conditions: Is the selected object a philosopher? If so, has the philosopher already been zapped by lightning? Conditional execution: lightning bolt will be shot or not

Multiple Conditions First, we’ll tackle the problem of restricting Zeus to shoot thunderbolts only at philosophers. This is different from previous examples in that four possible conditions must be checked – the user could click on any one of the four philosophers.

Demo Ch06Lec2bZeus-V1 Concept illustrated: Begin with just one object – we chose homer, but any one philosopher would do. Start with a blank If statement… drag in the who tile, then select == and homer

Demo Ch06Lec2bZeus-V2 Concept illustrated Boolean logic operators are used to build an expression composed of multiple conditions

Abstraction Multiple conditions, as in this example, become complex are difficult to read are difficult to debug A better solution is to write our own Boolean question that checks the conditions. This is another example of abstraction – allowing you to think on a higher plane.

Demo Ch06Lec2bZeus-V3 Concepts illustrated in this example Multiple conditions can be checked using nested If statements to check each possible condition. If one of the conditions is found to be true, the function immediately returns true and the function is all over! If none of the conditions are true, the function returns false.

Completing the Zeus world Now, we are ready to prevent lightning striking the same philosopher more than once. How do we know if a philosopher has already been struck by lightning? When a lightning bolt strikes, the philosopher “is zapped” and the his color property is set to black. Checking color is a convenient way to check for a previous lightning strike.

Demo Ch06Lec2bZeus-V4 Concept illustrated in this example We only need to check for a possible duplicate- strike if we already know that a philosopher is clicked the way to handle this is to nest a second If statement inside the first.