Repetition Control Structures

Slides:



Advertisements
Similar presentations
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010 modified July 2011.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Repetition Structures
While: Indefinite Loops Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we.
CS320n –Visual Programming Indefinite Loops (Slides 7-2) Thanks to Wanda Dann, Steve Cooper, and Susan Rodger for slide ideas.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University Feb. 24, 2007.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Line up By Melissa Dalis Professor Susan Rodger Duke University June 2011.
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Making a Timer in Alice.
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Events Chapter 7 Part 2. While a Key is Pressed Event Specialized event An event occurs when you press a key and continues until you take your finger.
Checking for Collisions: Alternative Method Erin Taylor Under the Direction of Susan Rodger July 2015 Duke University.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Making a Timer in Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University July
Mathematical Expressions, Conditional Statements, Control Structures
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Programming: Putting Together the Pieces Built-in Questions and Expressions Alice.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
Programming: Simple Control Structures
Simple Collision Detection By David Yan Under the direction of Professor Susan Rodger and Chari Distler Duke University, June 2015.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University July 19, 2007.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
CSC 107 – Programming For Science. Today’s Goal  Know how to use and write for loops  Explain why to use for, while, & do-while loops  Convert between.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Obj: Programming: Simple Control Structures HW: Read section 3 – 2 AC3 D2 Do Now: 1.Log on to Alice. Open the file firstEncounter.a2w located in the folder.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Flow Control: Repetition with While Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Programming: Simple Control Structures Sec 46 Web Design.
Repetition everywhere – comparing while in a method and as an event Susan Rodger Duke University July 2010.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
BILLBOARDS Display images Background Credits. We can put an image on a billboard and use is for a background Create a simple world Add characters Using.
Chapter 6 Controlling Program Flow with Looping Structures.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Jonathon Kuo Under the Direction of Dr. Susan Rodger
Creating a UFO Rescue Game in Alice
Chapter 6: Loops.
Programming: Simple Control Structures
Chapter 3: Decisions and Loops
Programming: Simple Control Structures
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Simple Control Structures
Control Structures
Creating a UFO Rescue Game in Alice
Iterations Programming Condition Controlled Loops (WHILE Loop)
Learning Java with Alice 3.0 Game Design Kathy Bierscheid
Programming: Simple Control Structures
CMSC 120: Visualizing Information 3/25/08
Outline Altering flow of control Boolean expressions
LRobot Game.
Programming: Simple Control Structures
Checking for Collisions: Using Functions in Alice
Programming: Simple Control Structures
Programming: Simple Control Structures
Computer Science Core Concepts
Programming: Simple Control Structures
Using Alice This is an introduction to Alice..
Python While Loops.
Chap 7. Advanced Control Statements in Java
Programming: Simple Control Structures
under the direction of Professor Susan Rodger
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Repetition Control Structures While Count

While control structure The WHILE statement executes instructions repeatedly in a loop while a condition is true. This conditional execution is also known as “repetition.” The WHILE condition: • Acts like a gatekeeper to an event. • Must be true to allow the programming instructions within the loop to execute.

WHILE Control Structure and Repetitive Execution statement(s) true boolean expression false The condition is implemented with a boolean expression

WHILE Control Structure and Repetitive Execution After all of the programming instructions within a loop are executed, the WHILE condition is evaluated again for repetitive execution. • If the condition is still true, execution will repeat. • If the condition is false, the loop will be skipped and execution will continue with the next programming statement following the WHILE control structure.

Relational Operators Relational Operators In most cases, the boolean expression, used by the while statement, uses relational operators. Relational Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to

Boolean Expressions Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y?

WHILE Control Structure   The WHILE control structure performs conditional loops using boolean expressions. • While a condition is true, the programming instructions within the loop are executed. • Once the condition is no longer true, program execution will skip the WHILE condition and continue with the programming instruction that follows the WHILE loop.

Conditional Execution The use of conditional control structures allows two types of loops:   Conditional loop: Stops when a condition is true. – Example: The propeller of a helicopter turns while the helicopter is moving or flying. If the helicopter stops, then the propeller stops turning.

Conditional Execution   Infinite loop: Never stops. – Example: The Hour And Minute Hands on a clock continue rolling.

WHILE Control Structure Process Flow The Queen moves forward, unless she collides with the Playing Card. If the Queen collides with the Playing Card, she stops and turns to face the camera Queen moves forward true No collision? false Queen stops and faces camera

Steps to Program a WHILE Control Structure   Drag and drop the WHILE control structure into the Code editor and select the true condition as a placeholder. Replace the true condition placeholder with the condition to evaluate. Insert procedures that will be executed while the condition is true. Insert the procedures that are executed after the while loop stops executing.

The Queen moves forward, unless she collides with the Playing Card The Queen moves forward, unless she collides with the Playing Card. If the Queen collides with the Playing Card, she stops and turns to face the camera Queen moves forward true No collision? false But wait, I want No collision

Lecture Practice #5 Create a .docx file with your name, the date, and your class (CMSC100) in the upper right corner. “Lecture Practice #5” should be centered. Name the file as follows:<your blackboard username>_LecturePractice5.docx When you have completed all the tasks, upload your .docx file to the blackboard website under Lecture Practice 5 Task #1 – Create an Alice world. Place the Queen of Hearts and a Playing card in the scene. Take a screenshot and paste it into the .docx file with the heading Task #1. Task #2 – Go to the code editor. Have the Queen turnToFace the Card. Add the following statements. Comment your code.Take a screenshot of the MyFirstMethod box and paste it into the .docx file with the heading Task #2.

Notice the relational operators

Lets change this to: As long as the Queen is more than 1 meter from the Playing Card, the Queen moves forward. If the distance between the Queen and the Playing Card is less than 1 meter, she stops and turns to face the camera Queen moves forward true Distance > 1 meter? false Queen stops and faces camera

Using relational operators Drag while into myFirstMethod – select true Select the arrow next to true and then select Relational (Decimal Number) . . . . Select the > and then select two numbers (these will be space holders)

There is a getDistanceTo function

Notice these two shapes match

Drag the getDistanceTo function to the left of the >

Change this to a 1.0 Solution

count control structure The COUNT statement executes instructions repeatedly in a loop a designated number of times. It uses a integer literal or a function that returns an integer literal to count the number of times to repeat.

Lecture Practice #5 Task #3 – Use the Count control structure to have the Playing Card jump up and down 5 times. Comment this portion of your code. Take a screenshot of the MyFirstMethod box and paste it into the .docx file with the heading Task #3.