Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.

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

Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
The If Then Else Statement Using JavaScript TIP The If Statement is the fundamental control structure that allows branches in the flow of control in a.
Selection Control Structures Chapter 5: Selection Asserting Java © Rick Mercer.
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.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS0004: Introduction to Programming Repetition – Do Loops.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
Copyright © Texas Education Agency, Computer Programming For Loops.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
CSC 1701B Computing: Science and Creativity. Outline  Types  Variables  Operators  Control: sequence, selection, repetition  Functions (block headings.
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.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Bug Session Four. Session description Objectives Session activities summary Resources Prior knowledge of sequencing instructions using Bug Bug website.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Decision Making: while loops and Boolean Logic. While Loops A while loop is a structure within ROBOTC which allows a section of code to be repeated as.
Sensor Information: while loops and Boolean Logic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 3: Decisions and Loops
The switch Statement, and Introduction to Looping
Web Programming– UFCFB Lecture 16
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Iteration with While You can say that again.
Selection CIS 40 – Introduction to Programming in Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops and If-Else Structures
LRobot Game.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Repetition Control Structure
Computer Science Core Concepts
ICT Programming Lesson 3:
Seating “chart” Front - Screen rows Back DOOR.
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.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
How to allow the program to know when to stop a loop.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Control Structures.
Presentation transcript:

Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity

The code we’ve considered executed all statements sequentially Correct steps in the correct order is our first step to programming We often need our programs to choose what to do based on changing situations Guarded Action pattern: Only do it if a Boolean expression is true Alternative selection: Choose one action or another CONDITIONALS: TO DO OR TO DO 2

3 Pattern: Guarded Action Problem: Execute an action only under certain conditions General if (true-or-false-condition is true) Form execute a set of blocks CodeExample: THE GUARDED ACTION PATTERN

FLOWCHART VIEW OF GUARDED ACTION After the boolean expression of the if statement evaluates, the true- part executes only when the boolean expression is true. boolean expression False block-1 block-n True Part

A boolean-expression a.k.a. predicate in Snap! is any expression that evaluates to true or false Snap! Has 3 predicate blocks Snap has 3 boolean operators: and or, not BOOLEAN EXPRESSIONS (PREDICATES) 5

COMPLETE TRUTH TABLE 6

Situations arise that require a program to select between one set of actions or another Withdraw or deposit money Pass or fail the entrance requirements This is the Alternative Action Pattern Choose between two alternate sets of actions ALTERNATIVE SELECTION 7

ALTERNATIVE ACTION Pattern: Alternative Action Problem: Must choose one action from 2 alternatives Outline: if (true-or-false-condition is true) execute action-1 execute action-1 else else execute action-2 execute action-2CodeExample:

FLOW CHART VIEW OF ALTERNATIVE ACTION 9 logical expression False block-1 block-n block-1 block-n True False Part True Part

We often need our programs to perform things repeatedly “Forever” Keep it going pattern A specific number of times, when we already know Determinate loop pattern Until something goal happens to stop the looping Indeterminate loop pattern LOOPING: RUN THE SAME TASKS REPEATEDLY 10

Pattern Name: Forever Loop Problem: Need to do something happening while the program is running Solution: SNAP ! forever block FOREVER LOOPS 11

Pattern Name: Determinate Loop Problem: Need to do something exactly n times, where n is already known Solution: Snap ! repeat or for blocks DETERMINATE LOOPS 12 // Java code (not on the test) Turtle turtle = new Turtle(0, 0); int n = 5; repeat { turtle.move(100); turtle.turnLeft(72); n = n - 1; } until (n == 1);

This for block is provided when a changing variable ( length below) is needed DETERMINATE LOOPS WITH THE FOR BLOCK 13

In the current version of SNAP !, the for block isn't installed automatically You will need to import it once for each new SNAP ! project in which you use it To do so, click on the icon at the top of the screen and select Import Tools NEED TO IMPORT TOOLS FOR THE FOR LOOP 14

Take one minute to write down what this script “says” ACTIVE LEARNING 15

Pattern Name: Indeterminate Loop Problem: Need to do something repeatedly until a goal is reached Code Demo: Ask the user for a even number in a given range Will use a repeat until block Will use a custom made block to abstract a complex boolean expression INDETERMINATE LOOPS 16