140 likes | 161 Views
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.
E N D
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)
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
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
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
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
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