Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Control Structures

Similar presentations


Presentation on theme: "Simple Control Structures"— 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) 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

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

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 This Action Do Something Else 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 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

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


Download ppt "Simple Control Structures"

Similar presentations


Ads by Google