1 / 14

Simple Control Structures

Understand how to use simple control structures, including conditional statements and relational operators, with examples in Alice programming environment. Learn to create if/else statements and execute conditional animations.

kimboyd
Download Presentation

Simple Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Control Structures

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

  3. 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)

  4. 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)

  5. 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

  6. 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 Something Else Do This Action Is condition True Do this Action

  7. 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

  8. 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

  9. The Resulting Animation

  10. 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)

  11. 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

  12. 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

  13. The Resulting Statement

  14. 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

More Related