Summer Computing Workshop. Session 4 Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent.

Slides:



Advertisements
Similar presentations
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
CS0007: Introduction to Computer Programming
CS0004: Introduction to Programming Repetition – Do Loops.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Intro to Programming Algebra-Geometry. Computer Programming? What is programming? The process of writing, testing, and maintaining the source code of.
Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Topic 6 – Repetition and Loops. CISC 105 – Topic 6 Program Repetition Repetition refers to the repeats of certain program statements within a program.
Copyright © Cengage Learning. All rights reserved. CHAPTER 2 THE LOGIC OF COMPOUND STATEMENTS THE LOGIC OF COMPOUND STATEMENTS.
The Program Design Phases
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
Loops and Switches. 1. What kind of blocks are these? 2. Name two kinds of controls that can be specified to determine how long a loop repeats. 3. Give.
Python quick start guide
Introducing Java.
Fundamentals of Python: First Programs
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programming & Scratch. Programming Learning to program is ultimately about learning to think logically and to approach problems methodically. The building.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Summer Computing Workshop. Session 2 Input in Scratch  Multi-Character input - This is used when the user is prompted to enter a number or word.  Problems.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
CPS120 Introduction to Computer Science Iteration (Looping)
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Section 1 Introduction National 4/5 Scratch Course.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 There are times when you will want blocks to repeat. Instead of duplicating blocks and ending up with a long script that might be confusing, there.
Algorithms Writing instructions in the order they should execute.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
TRIGGERS Triggers tell a script to start executing There are four types of triggers: When green flag is clicked When I am clicked When is pressed When.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
CPS120 Introduction to Computer Science Iteration (Looping)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Using Control Structures. Goals Understand the three basic forms of control structures Understand how to create and manage conditionals Understand how.
Controlling Computers with Programs When you create a computer program you are creating a set of instructions that tell the computer exactly and completely.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
ICS124 Session 9 Flowcharting 1. By the end of this section the student will be able to:  Name the three structures of the Structure Theorem  Identify.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
FOP: While Loops.
Chapter 7 JavaScript: Control Statements, Part 1
Programming & Scratch.
Scratch: iteration / repetition / loops
Quick Test What do you mean by pre-test and post-test loops in C?
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Expressions and Control Flow in JavaScript
Unit# 9: Computer Program Development
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Coding Concepts (Basics)
Computer Science Core Concepts
ICT Programming Lesson 3:
Using Decision Structures
Copyright © Cengage Learning. All rights reserved.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

Summer Computing Workshop

Session 4

Loops  At the heart of their functionality, loops enable blocks of code to be executed more than once  Loops represent a simple concept that can be used to create extremely dynamic and flexible programs  Loops are very important for two reasons: efficiency and flexibility  In the project of last session, you were instructed to basically run the program twice. In order to accomplish this you had to copy the existing blocks and append them to the end of the script effectively doubling the number of blocks present. It worked, but just imagine what would happen if you had to go through the script ten, one-hundred, or even a thousand times! Assuming you were able to create a program of that magnitude, what if a change had to be made? You would be required to make that change for each copy you made! Loops allow the program to contain only one copy of the code and go through it as many times as you need to. This is much more efficient

Repeat  In scratch, the simplest loop is the repeat block. It closely resembles the for loop found in other programming languages  The repeat block expects the programmer to specify a number which will indicate exactly how many times it will repeat  In this example, the move block will be repeated ten times. To find out how far the sprite will move in total, all we have to do is some quick multiplication to realize it will move 100 steps  This block is used when the number of times to repeat is known before the block is encountered, but sometimes, there is no way of knowing exactly how many times a loop must execute. In cases of uncertainty, a slightly different loop must be used

Repeat until  The repeat until loop, as the name implies, repeats until a given Boolean expression is true  It is very similar to the while loop found in other programming languages; the only difference is the Boolean expression is switched. Instead of repeating until a value is true, a while loop repeats until a value is false  Repeat until loops are perfect when combined with user input  This allows the user to interact with the program indefinitely; loops such as these are found in almost every program in some way, shape, or form  Although the two loops mentioned thus far appear to behave differently, they are the same loop. With the use of variables, the repeat until loop can be made to work exactly like a repeat loop

Forever  In some cases, a loop must continue repeating the entire time the program is running. When this is required a forever loop is often used  Notice that there is no “connector” on the bottom of this block. This is because any blocks located below it in the script would be impossible to reach  Like the repeat until loop, the forever loop is commonly used in conjunction with user input.

Forever  In session 2, we used the “when key is pressed” blocks to move a sprite around on the screen with the arrow keys; however, we were unable to move diagonally by pressing two keys at once  With the use of loops, multi-key input is now possible New and improved designLimited functionality from Session 2

Forever if  Scratch has one more loop that deserves a mention; the forever if block  It lacks a connector on the bottom like the forever loop but is able to accept a conditional block  After a bit of testing (I encourage you to test this as well) we realized it was just a combination of two other flow control blocks; the forever and the if blocks  These two scripts behave in exactly the same way =

Nesting  Like the conditional branch structures in the previous session, loops can, and sometimes must, be nested. The behavior of these loops is often trivial, but sometimes they can be very complex  This script moves the sprite 100 steps (2 * 10 * 5)  What does this one do?

On Your Own  Write a script that draws a box of a user-defined size on the screen using the pen tool. Create a 5x5 grid of these boxes

Session 4 Questions 1.The expression in the predicate part of an if/else structure evaluates to what kind of value? 2.What part of the structure would execute if the predicate value was true? 3.In your own words, what does a loop do? 4.One type of single-character input can be used to start a script, while the other can be used ______ the script. 5.If a certain block of number of blocks needed to be executed several times, what control structure would you use? 6.If a loop executes 5 times and there was a block inside that moved the sprite 3 steps, how many steps would it move in total?