02Loops - Loops Comp106 Loops • What if we wish to issue...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Loops Comp106 Loops • What if we wish to issue the same instructions to the robot multiple times? • We can write a sequence of commands (Fig. 1) or write a loop (Fig. 2) or write a function (will cover later). • Which is preferable? Fig.1 Fig.2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Types of Loops • 4 basic kinds of loops in RCX: Repeat For n times – fixed number of iterations. Repeat Forever – infinite loop. Repeat While a condition is present (condition-based). Repeat Until a condition is met (event- based). Repeat For Loops 1. Repeat for 4 times: 1.1 Forward for 1 second 1.2 Turn right for 1.5 second 2. EndRepeat repeat (4) { forward(100); turn_right(150); } int count = 1; while (count <= 4) { forward(100); turn_right(150); count = count + 1; } Pseudo-code Proper indentation is very important when writing code! NQC code
Background image of page 2
Forever Loops 1. Repeat forever: 1.1 Forward for 1 second 1.2 Turn right for 1.5 second 2. EndRepeat while (true) { forward(100); turn_right(150); } Writing in three “languages”
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

02Loops - Loops Comp106 Loops • What if we wish to issue...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online