PLTW ENGINEERING
Activity 3.1.4 While Loops and If-Else Structures
INTRODUCTION
One of the powerful attributes of a computer program is its ability to make decisions. Although it can
be argued that only humans are capable of decision-making, computers are able to make decisions
using criteria. They are able to compare two values and determine whether one is larger than the
other. They can determine whether a statement is true or false, based on empirical data.
EQUIPMENT
● Computer with VEXcode software
● VEX® POE V5 Testbed
RESOURCES
While Loops and If-Else Structures: VEXcode Software
Flowcharts
Procedure
1. Form teams of two and get your team’s POE testbed from your teacher.
2. Connect the VEX POE V5 testbed to your PC.
Figure 1. VEX POE Testbed
3. Open Lesson3_1_testbed_template and click File > Save As, select the folder that your
teacher designated, and then name the file “A3_1_4_Part1_forever”.
Part 1: Looping
In the programming world, sometimes you want to have a program perform an action forever, like wait
for some input. Humans do this when we listen for a phone call or text alert or when we listen to our
bodies for when it is time to eat. When you do something forever in a program it is called an infinite
loop.
Two versions of infinite loops:
Figure 2. Infinite Loop Figure 3. While Infinite Loop
The while loop is a construct that repeatedly executes code as long as a condition remains true. In
Figure 3, the condition is 1=1. Because 1 always equals 1, the condition is always true and the loop
never ends. Most programming languages have a while statement with the same basic structure.
Review the images below to see how the while keyword and the condition are related.
Following is an example of a forever loop that will turn on the LED for 2 seconds whenever the
bumper switch is pressed. The code inside the forever loop waits until the condition is met before it
continues, and the forever switch keeps checking if the bump switch is pressed, even after it has
been pressed multiple times.
Figure 4. Example of a Forever Loop
4. Build out the provided code. To test your program, press the bumper switch multiple times.
What is the purpose of a forever loop, and how might it make programming easier? Could
using a forever loop cause any problems with code?
ANSWER: The purpose of the forever loop is to provide an easier way of running the
program without it stopping.
Part 2: The Mission of Condition
A notable strategy in programming is the use of conditional statements. Conditional statements, also
called conditionals, allow you to write code that performs different actions depending on whether a
condition is true or false.
Another Name for Blocks: The blocks you write in VEXCode can also be referred to as instructions
or statements. Programmers often use the terms blocks, statements, and instructions
interchangeably.
5. List three conditional statements that occur in your everyday life as you make decisions. To
help you brainstorm, think about all the decisions you make from the time you wake up until
you go to bed.
ANSWER: second guessing, explanation, and rational thinking.
One type of conditional used to determine code execution is an if-then statement.
6. Observe the if-then block, which is found in the Control section of the Code menu.
Note: The image below is like a flashcard. Click to turn it over and read more about if-then
statements.
Code inside an if-then block will execute once only when the condition evaluates to true. For
example, games that have a life counter demonstrate if-then statements. As a player moves through
a game, the game keeps track of the life counter. If a player fails a segment or level of the game, their
life counter is reduced by 1. Looking at the life counter algorithm below, you can see the logic of an
if-then statement.
set life counter to 3
repeat until life counter is 0
play game
if level failed then
reduce life counter by 1
You can also create a flowchart of this algorithm.
Figure 5. Flowchart of a Game Counter Algorithm
7. Click File > Save As, select the folder that your teacher designated, and then name the file
“A3_1_4_Part2_If”.
8. Create the program below in your A3_1_4_Part2 file.
Figure 6. If Statement in a Loop
9. Download and run the program. Observe the behaviors as you turn the potentiometer.
10. Add a second if-then statement to the loop that checks if the potentiometer has a value of
0. Inside the if statement, add a spin rightMotor forward for 180 degrees block.
11. Download and run the program. As you turn the potentiometer, observe the behaviors.
Document this program with pseudocode and comments.
ANSWER: It behaves they same way we programed it on the project
12. Look at the programs in Figures 7 and 8. Predict what each program would do if you turned
the potentiometer, then pressed the bumper switch.
Figure 7. Forever Loop Using Wait Until Statements
Figure 8. Forever Loop Using If-then
13. Build each program and test them by turning the potentiometer first, then pressing the
bumper switch.
● Did the programs behave as you expected them to? Why or why not?
ANSWER:Yes it behaved as i expected
● Would the differences in these programs be noticeable if you turned the potentiometer
first, then pressed the bumper switch?
ANSWER: Yes because more work would have been involved
● Compare and contrast the differences in the way the programs respond to inputs when
using a wait until and if-then statements.
ANSWER: When responding to inputs, there is a pause when using the wait statements
Part 3: If-Then-Else Conditions
You have previously used if-then statements to execute code when a condition is true. The if-then
statement can expand to include code for when a condition is false. This is called the else clause.
The else clause serves as a catch-all when no other condition is met.
Note: The image below is like a flashcard. Click to turn it over and read more about if-else
statements.
An if-else statement is one way to tell a computer to make a decision. With this command, the
computer executes one of two pieces of code, depending on whether the condition is true or false.
14. Click File > Save As, select the folder that your teacher designated, and then name the file
“A3_1_4_Part3”.
15. Move the blocks out from under the when started block to a comment block that explains
the code. Add a new comment block to the when started block to read “Part 3 Provided Code”.
16. Using an if-else statement instead of the if-then statement, create the following code.
Figure 9. Example If-Else Code Statement
17. Download and run the program. Observe the behavior of the motors as you turn the
potentiometer.
Explain why a single if-else structure might be preferable to using two if structures.
ANSWER: because one single if-else structure works the same way two if structures do
Part 4: Your Turn
18. Open Lesson3_1_testbed_template and click File > Save As, select the folder that your
teacher designated, and then name the file “A3_1_4_Part4”.
19. Write pseudocode for a program that performs the following behavior:
a. When the limit switch is pressed, the left motor velocity responds to the potentiometer.
b. When the limit switch is pressed and an object is found with the optical sensor or the
distance sensor, only the right motor turns.
Note: Both the optical sensor and distance sensor can “find an object”. Program using
whichever sensor you have on your testbed.
c. When the limit switch is not pressed, the motors should always be off.
d. The program should loop indefinitely, waiting until the limit switch is pressed again.
20. Using VEXcode V5, code, download, and test your program.
21. Troubleshoot until the expected behaviors occur. Make sure your code is documented.
22. Save and share your code with your teacher.
Part 5: Additional Practice
You will now create programs to accomplish the following tasks.
23. Review your first task from the list below:
a. Make a motor spin as long as the bumper switch is held down. Its direction depends on
whether a person is within 20 mm of the distance sensor. If the button is released, the
behavior repeats when it is pressed again.
b. Make the servo position itself to the left or right depending on whether the optical sensor
is covered by your hand.
c. Make the servo position itself to the left or right depending on whether the optical sensor
is covered by your hand, but only if the button is pressed. If the button is released, the
servo is in a middle position, awaiting the button to be pressed again.
d. Make one motor spin whenever the bumper switch is pressed and a second motor spin
whenever a limit switch is pressed. This behavior repeats indefinitely, because the two
actions are independent.
24. Develop pseudocode for your program.
25. Open your Lesson3_1_testbed_template and save as a new file, such as A3_1_4_Part5A
(or 5B, and so on).
Note: Remember to do this before you begin to code for each task.
26. Develop, download, and test your program as necessary until your code correctly
completes the assigned task.
27. Share the code as directed by your teacher.
CONCLUSION
1. Describe any challenges that you encountered while developing the programs.
ANSWER: How to run and setup the programs
2. Describe one application each for a While loop and If-Else structure.
ANSWER: A while loop could be used when a specific button is pressed and opens up a
menu. A if-else could be used for background in video games