Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.

Slides:



Advertisements
Similar presentations
Chapter 4: Control Structures I (Selection)
Advertisements

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.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
CS0004: Introduction to Programming Repetition – Do Loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Python quick start guide
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.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
© Jalal Kawash Programming Peeking into Computer Science 1.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
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.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Summer Computing Workshop. Session 3 Conditional Branching  Conditional branching is used to alter the normal flow of execution depending on the value.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
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.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Sensor Information: while loops and Boolean Logic.
Control Flow (Python) Dr. José M. Reyes Álamo.
Programming & Scratch.
Python: Control Structures
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?
Simple Control Structures
Lecture 07 More Repetition Richard Gesick.
Selection By Ramin && Taimoor
Control Structures - Repetition
Loop Control Structure.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Objectives After studying this chapter, you should be able to:
Design and Implementation
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 6: Repetition Statements
Visual Basic – Decision Statements
Selection Statements.
Chapter 4: Control Structures I (Selection)
ICT Programming Lesson 3:
Decision I (if Statement)
Boolean Expressions to Make Comparisons
Variables & getting info from the user
Using Decision Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Repetition Statements (Loops) - 2
statement. Another decision statement, , creates branches for multi-
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.
Presentation transcript:

Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen

Selection (branching) – if Command Selection or Branching is computer programming jargon for choosing whether a line of code or a group of lines of code should be executed (run). It is often combined with repetition (repeat commands) as shown in the example below.

Conditions for Conditionals We can find hexagonal shaped conditions under the Sensing (light blue) group and the Operators (green) group. One of the easiest tasks we can do is to ask for some input and make a choice depending on what was entered.

Conditionals: one way branch - If Sometimes you wish to execute a section of code only when a condition is true. The code inside the “if” block is executed only when the condition is true. When the condition is false, the program continues executing any code below the “if” block. This is called a “one way branch”, as shown in the example below.

Conditionals: two way branch – If Else Sometimes you wish to execute one section of code when a condition is true, and a different section of code when the condition is false. The code following the “if” is executed when the condition is true, while the code following the “else” is executed when the condition is false. This is called a “two way branch”, as shown in the example below.

Conditionals: Multi way branch – If Else if Sometimes there are more than two selections of code that you wish to choose to execute depending on certain conditions. This is called a “multi way branch”, as shown in the example to the right. It is constructed by inserting “if” or “if else” blocks inside an “else” block. These are called “nested” or “extended” if statements.

Sequential selection Sequential selection is a list of “if” statements without “else if” statements. It is a good idea to avoid sequential selection whenever possible. Let’s compare a multi-way branch with sequential selection. Sequential selection is shown on the left of the two code samples. The logic is easier to follow, but the code is not as efficient, because the computer has to evaluate all of the if statement conditions every time the code is run.

Logical operators & compound conditions There are 3 logical operators in Scratch, “and”, “or”, “not”. “Not” means the opposite of the condition or Boolean expression that follows. In an “or” statement, if either condition is true, or both conditions are true than the entire Boolean expression (compound condition) is true. In an “and” statement, both conditions have to be true in order for the compound condition to be true. Logical operators (compound conditions) can be used in ‘if” statements AND in Repeat Until statements.

Using compound conditionals Compound conditionals can make your code shorter and more efficient. Consider the following equivalent sections of code. Both tell the sprite to rotate 180 degrees if the sprite’s “x position” is greater than 150 OR if the sprite’s “x position” is less than -150.

compound conditionals: error trapping This example uses a compound conditional in a Repeat Until statement to “error trap” input from the user (check for valid input within an acceptable range). Remember, “and” means BOTH conditions must be met. Ask the user for input first. Then check if the input is within the expected range in the condition of a Repeat Until. Give the user feedback if the input is not within the range, and ask the user for the information again in the “body” of the repeat until loop.