Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition Control Structures

Similar presentations


Presentation on theme: "Repetition Control Structures"— Presentation transcript:

1 Repetition Control Structures
While Count

2 While control structure
The WHILE statement executes instructions repeatedly in a loop while a condition is true. This conditional execution is also known as “repetition.” The WHILE condition: • Acts like a gatekeeper to an event. • Must be true to allow the programming instructions within the loop to execute.

3 WHILE Control Structure and Repetitive Execution
statement(s) true boolean expression false The condition is implemented with a boolean expression

4 WHILE Control Structure and Repetitive Execution
After all of the programming instructions within a loop are executed, the WHILE condition is evaluated again for repetitive execution. • If the condition is still true, execution will repeat. • If the condition is false, the loop will be skipped and execution will continue with the next programming statement following the WHILE control structure.

5 Relational Operators Relational Operators In most cases, the boolean expression, used by the while statement, uses relational operators. Relational Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to

6 Boolean Expressions Boolean Expressions A boolean expression is any variable or calculation that results in a true or false condition. Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y?

7 WHILE Control Structure
The WHILE control structure performs conditional loops using boolean expressions. • While a condition is true, the programming instructions within the loop are executed. • Once the condition is no longer true, program execution will skip the WHILE condition and continue with the programming instruction that follows the WHILE loop.

8 Conditional Execution
The use of conditional control structures allows two types of loops: Conditional loop: Stops when a condition is true. – Example: The propeller of a helicopter turns while the helicopter is moving or flying. If the helicopter stops, then the propeller stops turning.

9 Conditional Execution
Infinite loop: Never stops. – Example: The Hour And Minute Hands on a clock continue rolling.

10 WHILE Control Structure Process Flow
The Queen moves forward, unless she collides with the Playing Card. If the Queen collides with the Playing Card, she stops and turns to face the camera Queen moves forward true No collision? false Queen stops and faces camera

11 Steps to Program a WHILE Control Structure
Drag and drop the WHILE control structure into the Code editor and select the true condition as a placeholder. Replace the true condition placeholder with the condition to evaluate. Insert procedures that will be executed while the condition is true. Insert the procedures that are executed after the while loop stops executing.

12 The Queen moves forward, unless she collides with the Playing Card
The Queen moves forward, unless she collides with the Playing Card. If the Queen collides with the Playing Card, she stops and turns to face the camera Queen moves forward true No collision? false But wait, I want No collision

13

14 Lecture Practice #5 Create a .docx file with your name, the date, and your class (CMSC100) in the upper right corner. “Lecture Practice #5” should be centered. Name the file as follows:<your blackboard username>_LecturePractice5.docx When you have completed all the tasks, upload your .docx file to the blackboard website under Lecture Practice 5 Task #1 – Create an Alice world. Place the Queen of Hearts and a Playing card in the scene. Take a screenshot and paste it into the .docx file with the heading Task #1. Task #2 – Go to the code editor. Have the Queen turnToFace the Card. Add the following statements. Comment your code.Take a screenshot of the MyFirstMethod box and paste it into the .docx file with the heading Task #2.

15 Notice the relational operators

16 Lets change this to: As long as the Queen is more than 1 meter from the Playing Card, the Queen moves forward. If the distance between the Queen and the Playing Card is less than 1 meter, she stops and turns to face the camera Queen moves forward true Distance > 1 meter? false Queen stops and faces camera

17 Using relational operators
Drag while into myFirstMethod – select true Select the arrow next to true and then select Relational (Decimal Number) Select the > and then select two numbers (these will be space holders)

18 There is a getDistanceTo function

19 Notice these two shapes match

20 Drag the getDistanceTo function
to the left of the >

21 Change this to a 1.0 Solution

22 count control structure
The COUNT statement executes instructions repeatedly in a loop a designated number of times. It uses a integer literal or a function that returns an integer literal to count the number of times to repeat.

23 Lecture Practice #5 Task #3 – Use the Count control structure to have the Playing Card jump up and down 5 times. Comment this portion of your code. Take a screenshot of the MyFirstMethod box and paste it into the .docx file with the heading Task #3.


Download ppt "Repetition Control Structures"

Similar presentations


Ads by Google