CONTROL STATEMENTS AND SELECTION STRUCTURES Do in order

  • Slides: 27
Download presentation
CONTROL STATEMENTS AND SELECTION STRUCTURES Do in order Do together If __ Arguments

CONTROL STATEMENTS AND SELECTION STRUCTURES Do in order Do together If __ Arguments

CONTROL STRUCTURES Available pre-defined control structures include: • Do in order • Count •

CONTROL STRUCTURES Available pre-defined control structures include: • Do in order • Count • While • For each in • While • Do together • Each in together

You can drag a control structure to my. First. Method before or after creating

You can drag a control structure to my. First. Method before or after creating the programming instructions that will be included in the control structure

CONTROL STATEMENTS Control Statement Description • Default control statement in Code editor. • Executes

CONTROL STATEMENTS Control Statement Description • Default control statement in Code editor. • Executes procedures in sequential order. Do In Order Do Together • Executes procedures simultaneously. • Used for simultaneous movements such as walking and sitting motions.

Example 1 – Do Together For example, a movement executed together could be as

Example 1 – Do Together For example, a movement executed together could be as simple as simultaneously raising both arms of a biped object from a hanging position to a straight arm overhead position.

Lecture Practice #4 Create a. docx file with your name, the date, and your

Lecture Practice #4 Create a. docx file with your name, the date, and your class (CMSC 100) in the upper right corner. “Lecture Practice #4” should be centered. Name the file as follows: <your blackboard username>_Lecture. Practice 4. docx When you have completed all the tasks, upload your. docx file to the blackboard website under Lecture Practice 4 Task #1 – Create an Alice world. Select any background you want. Go to the screen editor. Place a biped object on the screen (person, bunny, Cheshire cat, etc). Leaves the arms down. Take a screenshot and paste it into the. docx file with the heading Task #1. Task #2 – Go to the code editor. Raise arms of your bipend straight up like in the example at the same time. Take a screenshot of the My. First. Method box and paste it into the. docx file with the heading Task #2. Comment your my. First. Method. Example

 Example 2 – Do Together Another example is a walking motion, which requires

Example 2 – Do Together Another example is a walking motion, which requires simultaneous movement of the hips and shoulders. To create the walking motion for a biped, use: • A series of move, roll, and turn procedures. • Do together control statements. Different programming may be required for different objects because no two objects walk the same way

Walking motion algorithm

Walking motion algorithm

Walking motion implementation

Walking motion implementation

 When Procedures Offset Each Other A common mistake is to include two procedures

When Procedures Offset Each Other A common mistake is to include two procedures that will offset and cancel each other in a Do together construct. For example, if you include a move up 1 meter procedure, followed by a move down 1 meter procedure in a Do Together, nothing will happen. The procedures cancel each other out.

Boolean • Boolean type is either true or false • Boolean variable can hold

Boolean • Boolean type is either true or false • Boolean variable can hold either true or false • Boolean functions return either a true or false

Boolean expression • An expression that evaluates to either true or false • Many

Boolean expression • An expression that evaluates to either true or false • Many Boolean expressions use relational operators Relational Operator Name Value Produced value 1 == value 2 equality If value 1 equals value 2 then result is true, else false value 1 != value 2 inequality If value 1 is not equals to value 2 then result is true, else false value 1 < value 2 less than If value 1 is less than value 2 then result is true, else false value 1 <= value 2 Less than or equal If value 1 is less than or equal to value 2 then result is true, else false Value 1 > value 2 Greater than If value 1 is greater than value 2 then result is true, else false Value 1 >= value 2 Greater than or equal If value 1 is greater than or equal to value 2 then result is true, else false

Boolean • Notice the shape – it represents a Boolean value

Boolean • Notice the shape – it represents a Boolean value

CONDITIONAL EXECUTION CONTROL STRUCTURES Conditional control structures allow you to control execution based on

CONDITIONAL EXECUTION CONTROL STRUCTURES Conditional control structures allow you to control execution based on a condition, or a decision being made. Consider these examples: If the current color of an object is blue, change it to orange. If the width to the rock is less than 1 meter, move forward 1/2 meter. If the object opacity is 0, change the opacity to 1.

IF Control Structure The IF control structure requires a Boolean condition of true or

IF Control Structure The IF control structure requires a Boolean condition of true or false when the structure is dragged into the program. • This initial condition is a placeholder. You will need to establish the condition to be evaluated.

 IF Control Structure Parts The IF control structure has two parts: the IF

IF Control Structure Parts The IF control structure has two parts: the IF part, and the ELSE part. • If the IF part is executed, the ELSE part is never executed. • If the ELSE part is executed, the IF part is never executed. • Both the IF and ELSE part of the IF statement can contain another nested If control structure

IF Control Structure Process Flow If it is cold outside I will wear a

IF Control Structure Process Flow If it is cold outside I will wear a coat, otherwise I will wear shorts. No Wear shorts. Is it cold outside? Yes Wear a coat.

IF Control Structure Process Flow Example If the cat walks into the dog, then

IF Control Structure Process Flow Example If the cat walks into the dog, then the cat says “Excuse me!” Otherwise, the cat continues walking. No Cat keeps walking Does cat collide with dog? Yes Cat says “Excuse me”

Steps to Program an IF Control Structure 1. Insert the initial motions that happen

Steps to Program an IF Control Structure 1. Insert the initial motions that happen before the IF control structure is executed. 2. Drag and drop the IF control structure into the Code editor and select the true condition as a placeholder during setup.

Steps to Program an IF Control Structure 3. Replace the true condition with a

Steps to Program an IF Control Structure 3. Replace the true condition with a condition to evaluate. 4. Insert the procedures that will execute if the condition is true (IF) and those that will execute if the condition is false (ELSE). 5. Run the animation to test the conditional behavior. Debug as necessary.

IF Control Structure Example If the cat collides with the dog, then the cat

IF Control Structure Example If the cat collides with the dog, then the cat moves backward and says “Excuse me!” Otherwise, the cat continues to move forward The is. Colliding. With function was dragged onto the true condition placeholder. This function tells us if one object is colliding with another

Notice the shapes match

Notice the shapes match

Lecture Practice #4 Task #3 – Add the Cheshire Cat and the scotty. Dog

Lecture Practice #4 Task #3 – Add the Cheshire Cat and the scotty. Dog to your scene. Have the cat turn. To. Face the dog and put them a short distance apart. Have the cat move. To the dog. Add the following statements to your my. First. Method. Comment this portion of your code. Run. Take a screenshot of the My. First. Method box and paste it into the. docx file with the heading Task #3.

ARGUMENTS The arguments of a procedure may be edited or further defined to control

ARGUMENTS The arguments of a procedure may be edited or further defined to control object movement and timing. Examples of Alice 3 arguments include: • Object • Direction amount • Time duration A computer program requires arguments to tell it how to implement the procedure.

Arguments Display Below are examples of arguments in a procedure.

Arguments Display Below are examples of arguments in a procedure.

Steps to Edit Arguments 1. Next to the argument’s value, click the arrow to

Steps to Edit Arguments 1. Next to the argument’s value, click the arrow to display the menu of values. 2. Select a new value. The menu indicates the current value, followed by pre-set values to choose from, followed by additional menu options to specify randomization, math calculations, or a custom decimal number.

Lecture Practice #4 Task #4 – Change the arguments for the turn methods of

Lecture Practice #4 Task #4 – Change the arguments for the turn methods of your biped object to have a duration of 1. 0 and Run. Set the animation. Style to: Now change the duration to 2. 0 and Run. Take a screenshot of the My. First. Method box and paste it into the. docx file with the heading Task #4.