Simple Control Structures

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

How Tall Are You? Introducing Functions By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made June 2014 by.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Repetition Structures
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Using Functions in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University July 2008.
Programming: Simple Control Structures Part 1 – Conditional Execution Alice.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Fall 2007ACS-1805 Ron McFadyen1 Chapter 3 Programming - Putting Together the Pieces.
Functions and Conditionals in Alice 1 Stephen Cooper Wanda Dann Barb Ericson September 2009.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Execution Control with If/Else and Boolean Questions Part 1 Alice.
Decision Structures Chapter 4 Part 2. Chapter 4 Objectives To understand o What relational operators are and how they are used o Boolean logic o Testing.
PAGES:51-59 SECTION: CONTROL1 : DECISIONS Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
Four Fundamental Pieces Instruction Control Structure Function Expression.
Mathematical Expressions, Conditional Statements, Control Structures
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
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
Programming: Simple Control Structures MMP 220 Multimedia Programming This adapted material was prepared for students in MMP220 as as part of a curriculum.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
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.
Programming: Simple Control Structures Sec 46 Web Design.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.
How Tall Are You? Introducing Functions for Alice 3 By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008 Updates made.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
 Type Called bool  Bool has only two possible values: True and False.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
JavaScript Controlling the flow of your programs with ‘if’ statements
Programming: Putting Together the Pieces Built-in Functions and Expressions Sec 8-5 Web Design.
Programming: Simple Control Structures
Chapter 4: Making Decisions.
Programming: Simple Control Structures
Control Structures
Repetition Control Structures
Microsoft Visual Basic 2005 BASICS
Programming: Simple Control Structures
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Design and Implementation
Checking for Collisions: Using Functions in Alice
Pages:51-59 Section: Control1 : decisions
Programming: Simple Control Structures
Using Functions
Using Functions in Alice
Selection Statements.
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
How Tall Are You? Introducing Functions
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Simple Control Structures
Pages:51-59 Section: Control1 : decisions
Programming: Simple Control Structures
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Programming: Putting Together the Pieces Built-in Functions and Expressions Alice.
Presentation transcript:

Simple Control Structures

What will we learn today? We will learn Simple Control Structures Conditional statement How an if/else statement is processed Example Relational Operators

Control Structures Control Structure: is a programming statement that allows you to control the order in which instructions are executed Examples Do together, do in-order Conditional execution (if/else statement) Repetition (loops)

Conditional Statement It is a control statement depends on a decision When a decision is being made a question is asked about certain condition in the world “e.g. is Ahmed taller than Ali?” The answer is a Boolean value either True (yes) or False (no) Some condition is checked and a decision is made about weather/not a certain section of program code is executed Decisions are useful when some instructions are executed to run only under certain condition

If/else Statement In Alice, if/else statement is used to control whether a block of instructions is executed or a method is called An if statement contains a ‘Boolean Condition’ used to determine whether the part of code will execute When the Boolean condition evaluates to True, the If part of the statement will execute (run) False, the else part will execute The statement checks to see weather a condition is true, if condition is true one statement of program instruction is run. Otherwise a separate set of a program is run

How an If/else Statement is Processed True False It is possible that no action would be expected for one of the parts in the flowchart True False Do This Action Do Something Else Is condition True Do this Action

Boy and Cat Example The cat is sitting on a table and the little boy wants to catch it If the boy is shorter than the table, he can jump to get the cat Else, he can hold the cat easily The question is how to know which is taller the little boy or the table? Use function is shorter than of the boy object

Create an If/else Statement How to create an if statement in Alice Drag the if/else tile into the editor Select the initial condition from the popup box Drag the boy’s is shorter than function and put it in the place of true in the editor If the answer is true, the if part is executed and else part skipped. If the answer is false, if part is skipped and else executed

The Resulting Animation

Relational Operators Alice provides 6 relational operators grouped together in the math category of world’s built-in functions They work the same way as they do in mathematics (== is equal to) (!= is not equal) (> is greater than) (< is less than) (>= is greater than or equal to) (<= is less than or equal to)

Boy and Cat Example Using a relational operator to write your own condition Assume that the boy is 1 meter tall, write if statement that checks the boy’s height against 1 meter If the boy’s height is less than 1 meter Do in order Boy moves up Boy moves down Else Do nothing

Create your own Boolean Expression Drag the if tile into the editor Drag the world function (a<b) on top of the true tile in the if statement From the popup menu select 1 for a and 2 for b as a placeholder Drag the boy’s height function to replace 1 Inside if part, add a do in-order block and instructions to make the boy jumps up Else, do nothing

The Resulting Statement

Let’s Work with Alice The SpiderRobot (Scific) is moving in the space, it encounters a rock and it wants to see if there is something behind the rock or not. Write an animation to implement this storyboard If the spiderRobot is shorter than rock Do in Order spiderRobot neck move up spiderRobot neck move down Else Do nothing