Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.

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

Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
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.
CS0007: Introduction to Computer Programming
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Selection (decision) control structure Learning objective
An Object-Oriented Approach to Programming Logic and Design
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
INTRODUCTION TO THE SCRATCH PROGRAMMING ENVIRONMENT.
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.
Ms. Deveny Second Semester  Introductions  Interview your table partner  Name  Why taking CPD  Computer experience  Favorite game?
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Institute for Personal Robots in Education (IPRE)‏ CSC 170 Computing: Science and Creativity.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Section 1 Introduction National 4/5 Scratch Course.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Summer Computing Workshop.  This workshop is designed to introduce basic programming concepts through the use of the highly intuitive programming environments.
Algorithms Writing instructions in the order they should execute.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
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.
JavaScript, Fourth Edition
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
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.
Decision Making and Branching
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.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Functions / Blocks.
Chapter 4: Making Decisions.
Scratch: iteration / repetition / loops
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 5 Decisions. Chapter 5 Decisions ssential uestion: How are Boolean expressions or operators used in everyday life?
Intro to CS Monday, August 29
Intro to CS Monday, August 24
Microsoft Visual Basic 2005 BASICS
Selection CIS 40 – Introduction to Programming in Python
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Selection Statements.
Chapter 4: Control Structures I (Selection)
Using Decision Structures
Presentation transcript:

Summer Computing Workshop

Session 3

Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value of a Boolean expression  The use of conditional branching allows programs to become much more flexible  The primary block associated with this behavior is the if block  The triangle shaped area is called the predicate or conditional part of the structure. This is what determines if the consequent (internal) blocks are executed  The triangle shaped blocks in Scratch always represent a true or false value. This “typing” is not as definite in Scratch as in other languages, but it is there nonetheless and certainly prepares you for the much more strict typing of languages you will encounter in the future.

Conditional Branching – If Block  Let’s take a look at an example  Starting at the top, the sprite will move 100 steps in whatever direction it’s pointing  Then the computer asks itself the question “Does 2 equal 3?” Obviously it does not so the green comparison block evaluates to false  Since the predicate is false, the turn block is skipped  The normal flow of execution is then resumed and the second move block is encountered  As a result, this script causes the sprite to move a total of 200 steps

Conditional Branching – If Block  What if we change the numbers so the comparison returns true?  Since 2 does equal 2, the comparison returns true and the turn block is executed  Then, as before, the normal flow of execution resumes and the remaining blocks beneath the if block are executed  Therefore, this snippet of code moves the sprite 100 steps, takes a 90 degree right turn, and then travels 100 more steps

Conditional Branching – If/Else Block  The if block isn’t the only block capable of branching based on a condition in Scratch. The if/else block is equally, and sometimes even more, useful.  The structure is very similar to the if block with the only difference being the addition of the “else” or alternate area  The top part of the if/else block behaves the same way the if block does, but while the blocks in the consequent area are executed if the condition is true, the blocks in the alternate (else) area are executed only if the condition is false  By definition, it is impossible for blocks contained in both the consequent and alternate areas to be executed. Once the decision is made, it is strictly one or the other

Conditional Branching – If/Else Block  Let’s look at an example of if/else block behavior  The sprite moves 50 steps then reaches the if/else predicate  The comparison evaluates to false since 5 is NOT less than 2  Due to the false value of the condition, the turn 45 block is skipped and the turn 135 block is executed instead  To make the sprite movements more visible we can use the pen tool to draw a line as the sprite moves

The Pen Tool  To make the sprite movements more visible we can use the pen tool to draw a line as the sprite moves  Click the pen category in the upper area of the blocks palette  Drag the block to the scripts area, but don’t link it to the existing script  Click the block, you should see a colored rectangle appear near the lock symbol in the sprite info area  Now, whenever the sprite moves, it’s path will be marked by a line. The size and color of this line can be altered if you want, but the default settings will work for now  After a bit of drawing, the stage can become quite colorful. When that happens, the block can be used to erase the lines

Conditional Branching – User Input  In the previous examples, the condition was unable to change. The comparison would evaluate to either true or false and stay that way forever because the relation of numbers never changes. Let’s modify the previous bit of code so the condition changes based on input from the user  With this small change, the true flexibility of the if/else structure begins to emerge  Now, every time the script is ran, a different branch can be taken. The number of choices is limited to two in this case, but through the power of nesting, this limitation is no longer an issue

Conditional Branching – Nesting  What if the question asked had more than 2 valid answers? A single if/else block is unable to handle a third result, and even if it could, there is no way to select it since all Boolean expressions give a true or false answer  This problem is addressed with a technique know as nesting. By placing if/else blocks inside of other if/else blocks, powerful decision trees can be created to handle any number of situations

On Your Own  Create a program that asks the user which direction the sprite should move, then prompt for a distance to move in that direction  Once you have completed the above project, modify it so the user is asked the questions twice in a row instead of just once (get direction, ask distance, move, ask direction, ask distance, move)

Session 3 Questions 1.How might the flow of execution be affected by a conditional branch? 2.What part of an if/else control structure determines which sequence of blocks is executed? 3.The two different types of input are single-character input and ______ input. 4.Asking for a name is an example of what kind of input? 5.The expression in the predicate (conditional) part of an if/else structure evaluates to a _____ or _____ value. 6.What part of the structure would execute if the predicate value was false?