Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

Programming with Microsoft Visual Basic th Edition
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
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 © 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.
Introduction to Flowcharting
Understanding the Three Basic Structures
CS0004: Introduction to Programming Repetition – Do Loops.
Repetition Structures
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Repeating Actions While and For Loops
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
CSC103: Introduction to Computer and Programming
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
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 3 Making Decisions
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
CPS120: Introduction to Computer Science Decision Making in Programs.
© Jalal Kawash Programming Peeking into Computer Science 1.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Mathematical Expressions, Conditional Statements, Control Structures
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running Types of data.
(C)opyright 2000 Scott/Jones Publishers Introduction to Flowcharting.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
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 C H A P T E R 5 Repetition Structures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding the Three Basic Structures Structure: a basic unit of programming logic Any program.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
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.
Programming: Simple Control Structures Sec 46 Web Design.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
CS320n –Visual Programming Execution Control with If / Else and Boolean Functions (Slides 6-2-1) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
REPETITION CONTROL STRUCTURE
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
Programming: Simple Control Structures
PROGRAM CONTROL STRUCTURE
Simple Control Structures
Repetition Control Structures
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Design and Implementation
Presentation transcript:

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean variable –what sequence structures are and when used –what decision structures are and when used –the difference between dual-alternative and single-alternative decision structures –what nested instructions are –what relational operators are and how they are used –what an infinite loop is –what conditional loops are

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-2 Today’s Agenda 1.Boolean Values 2.The If / Else Decision Structure 3.Relational Comparisons and Logical Operators 4.The Loop Instruction 5.The While Instruction

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-3 Boolean Variables Boolean Variables hold only two values –True –False –Initial Value is usually set to be “TRUE” 4.1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-4 Boolean Functions Boolean Functions return one value from the choice of –True –False Also primitive functions that are Boolean –User prompted to answer Yes (true) or No (false) 4.1

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-5 The If/Else Decision Structure If/Else decision executes one set of instructions if a Boolean condition is true and a different set if the condition is false All previous instructions have been consecutive (sequence structure), that is: –One –After –The –Other 4.2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-6 If/Else If/Else is a decision structure Instruction tests a condition (which gives a Boolean value) and then based on a true value does task “A” or task “B” if the value was false BOTH actions will NOT be performed…only ONE can be! If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If TRUE FALSE 4.2 The penguin falling or speaking are instructions that are conditionally executed –They do NOT always execute –Executed only under certain conditions

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-7 Decision Structure 4.2 Penguin falls down into the hole Flowchart If/Else Tile

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-8 If/Else Note to empty “slots” in instruction. –IF part –ELSE part The TRUE placeholder is replaced with the Boolean variable or a Boolean function Instructions tiles added to both the IF part and the ELSE part More than one instruction can be added to both the IF and ELSE part! Empty If/Else instruction 4.2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-9 Single-Alternative Decision Structures If/Else is a dual-alternative decision structure –Two paths of execution: one following TRUE and the other following FALSE Single-alternative decision structures are similar to the dual-alternative –The ELSE part is empty! 4.2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-10 Single-Alternative Decision Structures Start Penguin turns to face hole Hole wider than penguin? End Penguin walks to center of hole Penguin falls into hole 4.2 T F

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-11 Nested If/Else Instructions An If/Else instruction is placed inside another If/Else instruction or nested. The inner If/Else executes only if the outer If/Else is true. 4.2

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-12 Nested If/Else Instructions 4.2 Start Penguin faces hole Within 2 meters “Too Far!” True Hole wider? “Drats!”Penguin walks Penguin falls in End True False

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-13 Relational Comparisons Relational Operators used to compare values and determine whether relationships such as greater than, less than, or equal to exist Compare two values and determine how they relate to each other 4.3 OperatorMeaning = Equal to ! =Not equal to >Greater than > =Greater than or equal to <Less than < =Less than or equal to

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-14 Relational Operators Binary…operate on two pieces of data Can use to create conditions in an If/Else instruction Alice uses “a” and “b” for placeholders –Drag the desired tile and replace either the “a” or “b” with a value…such as an object 4.3

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-15 Logical Operators Tests more than true/false…can do complex testing! –Test two conditions to see if they BOTH are true! –Or ONLY one condition is true! –Or NEITHER condition is true! 4.3

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16 The Loop Instruction Loop instructions cause one or more other instructions to repeat or loop a certain number of times. 4.4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-17 The Loop Instruction The Loop instruction has an empty slot where instructions can be inserted. Look at the complicated version… 4.4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-18 Computing the Number of Times to Repeat What if you want the something to loop and NOT have to specify a particular number of repetitions? –For example…getting a ball to roll across the screen, regardless of where the ball is initially placed –Use the soccerBall’s distance function to calculate the distance –Loop uses integers (whole numbers) and decimal portions are dicarded –Objects loop 4 times, not 4.8; 3 times, not

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-19 Infinite Loops Infinite Loops…the loop NEVER stops! –Use for objects that shouldn’t stop! If placed in consecutive order…the next instructions will NEVER occur, since the loop NEVER ends! Place an infinite loop into a Do Together structure with other items. 4.4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-20 Flowcharting a Loop Instruction Set index to 0 Index < 12? Clock, minute roll left 1 revolution Clock, hour roll left 1/12th revolution Add 1 to index T 4.4

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Your turn – try these exercises Try exercises 3, 4, and 6 on pages Plane crash –Add a biplane object to the top of the world and have it fall from the sky –Use a conditional expression to check when the plane hits the ground –Have the plane say “Oops” when it hits the ground 4-21

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-22 The While Instruction While instruction is a loop that repeats as long as a Boolean condition is true Second repetition structure Called conditional loop…since the loop is controlled by a condition Let’s look at a tennis racket example 4.5

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-23 Flowcharting a While Instruction Loop’s condition is tested before each repetition of the loop First, it tests condition –If true…performs a repetition and starts over –If false…the loop terminates 4.5 Start Distance > 0? Racket turns forward Ball moves forward End

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Your turn – try these exercises Try the RescueSea tutorial (handout) Try the Roman Numeral Exercise –Create an interactive world that asks the user to enter a number that is at least 1, but not more than 10. Select an Alice character who will say the Roman Numeral equivalent of the number entered by the user. If the user enters a number outside the range of 1-10, the character should ask the user to enter another number Goal keeper –Create a world with a soccer net and a ball –Have the ball go into the goal as a goalkeeper dives to try to stop it 4-24