Repetition Control Structures While Count
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.
WHILE Control Structure and Repetitive Execution statement(s) true boolean expression false The condition is implemented with a boolean expression
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.
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
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?
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.
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.
Conditional Execution Infinite loop: Never stops. – Example: The Hour And Minute Hands on a clock continue rolling.
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
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.
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
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.
Notice the relational operators
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
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)
There is a getDistanceTo function
Notice these two shapes match
Drag the getDistanceTo function to the left of the >
Change this to a 1.0 Solution
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.
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.