Introduction to Robotics/Control Flow/Lecture/Teachers: Difference between revisions

wikademia>Whiteknight
New page: {{Introduction to Robotics/Nav|Control Flow}} == Control Flow == A computer can only perform one instruction at a time. In a program, we write the instructions that we want the computer ...
 
wikademia>Dr. Eme
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Introduction to Robotics/Nav|Control Flow}}
{{ntnes}}


== Control Flow ==
== Control Flow ==
Line 9: Line 9:
We have already seen the FOR / NEXT loop in the previous lab, but what does it do? A FOR / NEXT loop uses a counter variable to count the number of loops. Each time we perform the loop, we increase the value of the counter variable. The NEXT keyword moves the control flow back to the top of the loop. If your loop does not have a NEXT keyword, it will not repeat. Once the counter variable reaches the TO value, the loop exits, and the control flow continues after the loop.  
We have already seen the FOR / NEXT loop in the previous lab, but what does it do? A FOR / NEXT loop uses a counter variable to count the number of loops. Each time we perform the loop, we increase the value of the counter variable. The NEXT keyword moves the control flow back to the top of the loop. If your loop does not have a NEXT keyword, it will not repeat. Once the counter variable reaches the TO value, the loop exits, and the control flow continues after the loop.  


{{Introduction to Robotics/Visual|image=[Insert Image of a loop with arrows going up to the top, and out from the bottom]|caption=A FOR / NEXT Loop.}}
{{Introduction to Robotics/Visual|FOR NEXT Loop}}


We can “nest” FOR / NEXT loops. “nesting” means that we put one loop inside another loop. When we nest FOR / NEXT loops, we need to use different counter-variables for each loop.
We can “nest” FOR / NEXT loops. “nesting” means that we put one loop inside another loop. When we nest FOR / NEXT loops, we need to use different counter-variables for each loop.


{{Introduction to Robotics/Visual|text=
{{Introduction to Robotics/Visual|Nested Loops 1}}
MyCount1 VAR Byte
MyCount2 VAR Byte
FOR MyCount1 = 0 TO 200
FOR MyCount2 = 0 TO 200
...
NEXT
NEXT
}}


If we use the same counter-variable, the variable will be changing in the inner loop, and the outer loop will not work correctly:
If we use the same counter-variable, the variable will be changing in the inner loop, and the outer loop will not work correctly:


{{Introduction to Robotics/Visual|text=
{{Introduction to Robotics/Visual|Nested Loops}}
MyCount VAR Byte
FOR MyCount = 0 TO 200
FOR MyCount = 0 TO 50
...
NEXT
NEXT
MyCount never reaches 200
}}


=== How it Works ===
=== How it Works ===
Line 48: Line 29:
#If the counter variable is not equal to the TO value, go to #2
#If the counter variable is not equal to the TO value, go to #2


{{Introduction to Robotics/Visual|image=[Insert Image of how a loop works here]|caption=How a FOR / NEXT Loop Works.}}
{{Introduction to Robotics/Visual|FOR NEXT Control Flow}}


== Labels and GOTO ==
== Labels and GOTO ==
Line 54: Line 35:
Labels are like destination points. When we put a label in our code, we can jump to it using a GOTO statement.  
Labels are like destination points. When we put a label in our code, we can jump to it using a GOTO statement.  


{{Introduction to Robotics/Visual|
{{Introduction to Robotics/Visual|Labels and GOTO}}
[Insert an image here
}}


== Branches ==
== Branches ==