Execution Control with If/Else and Boolean Questions Example: Multiple Conditions 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

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Example: Multiple Conditions Execution Control with If Else and Boolean Questions Alice.
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.
Repetition Structures
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
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 Sec 52 Web Design.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
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.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Making Decisions. Understanding Logic-Planning Tools and Decision Making Pseudocode – A tool that helps programmers plan a program’s logic.
Flow of Control Part 1: Selection
Execution Control with If/Else and Boolean Functions Alice.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
ForLoopsInAlice1 Stephen Cooper Wanda Dann Randy Pausch Barb Ericson Oct 2009 Counted (For) Loops in Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
CS320n –Visual Programming Definite / Counted Loops (Slides 7-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Repetition: Definite Loops Sec 53 Web Design. Objectives The Student will: Understand loops and why they are used Understand definitive loops Know how.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
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.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Repetition: Definite Loops Alice. Repetition In many kinds of animations, especially simulations and games, some actions happen again and again. Example:
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
1 Quiz Show Programming Terms. 2 Alice - 3D Virtual Programming Vocabulary Quiz Board Chapter 1 Chapter 2a Chapter 2b Chapter 3 Chapter 4 $100 $200 $300.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Chapter 8: More on the Repetition Structure
Execution Control with If/Else and Boolean Functions
Chapter 3: Decisions and Loops
Javascript Conditionals.
JavaScript: Control Statements I
An Introduction to Programming with C++ Fifth Edition
Programming: Simple Control Structures
IF if (condition) { Process… }
CS320n –Visual Programming
Repetition: Definite Loops
Objectives After studying this chapter, you should be able to:
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Design and Implementation
Counted (For) Loops in Alice
Chapter 8: More on the Repetition Structure
Programming: Simple Control Structures
Repetition: Definite Loops
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Repetition: Definite Loops
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Computer Science Core Concepts
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
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.
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:

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

The Zeus world revisited Zeus-V1.a2w Testing this world, we find two significant problems Anything the user clicks on is 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 Zeus-V2 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 Zeus-V3 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 Zeus-V4 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 Zeus-V5 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.

Problem Create a flowchart to determine the price for a customer to an amusement park that has the following ticket prices: All DayEvening Adult2015 Child107.5 Senior129

Flowchart Symbols A reminder TrueFalse Conditional Branch if ( test ) … else … Statement assignment, method, etc. test

Flowchart to code Take the ticket price flowchart you have built, and convert it to code (actually pseudocode, because you should not worry about the details of syntax and grammar)

More complicated loops It is also possible to place a loop statement within another loop statement This is called nested loops

An example of nested loops The whole Ferris wheel will rotate clockwise, while the two inner wheels will rotate counterclockwise. The inner wheels should perform 2 revolutions for each outer loop revolution.

Demo FerrisWheel Concept illustrated in this example The inner loop runs completely each time the outer loop runs once. An outer loop that executes 2 times and an inner loop that executes 5 times will actually execute the inner loop 10 times.

Picking the corn It is time for a farmer to harvest his crops (corn). His field is nicely planted, with 6 rows of corn, and 10 cornstalks per row. Create a flowchart that will illustrate a solution to this problem Convert the flowchart to pseudocode.

Picking the corn v2 The farmer goes to the next field (of the same dimensions as the first) to harvest his corn. But the scarecrow has not done its job, and the crows have helped themselves to a lot of the corn. Each stalk of corn may or may not have an ear of corn to be picked Create a flowchart and that will illustrate a solution to this problem and convert the flowchart to pseudocode.

Assignment Read Chapter 6 Section 2, Execution control with If/Else and Boolean functions Read Chapter 7 Section 1, Loops