Computer Science Canada Loop situation |
Author: | redsp0t [ Mon Nov 15, 2010 4:30 pm ] |
Post subject: | Loop situation |
Hi, I have to do a program that repeats the numbers 1 to 15 in different colours, this may be easy but I am new to this and would like some explanation on how to do this. Thank you. |
Author: | TheGuardian001 [ Mon Nov 15, 2010 5:45 pm ] |
Post subject: | Re: Loop situation |
the color() method will let you set the text colour. a for loop will let you do something 15 times, and gives you a variable to output the put command will let you output the number. |
Author: | SNIPERDUDE [ Mon Nov 15, 2010 6:35 pm ] |
Post subject: | RE:Loop situation |
The Turing Walkthrough has plenty of information on how to use the colour and for loop commands. |
Author: | redsp0t [ Mon Nov 15, 2010 9:00 pm ] |
Post subject: | Re: Loop situation |
TheGuardian001 @ Mon Nov 15, 2010 5:45 pm wrote: the color() method will let you set the text colour.
a for loop will let you do something 15 times, and gives you a variable to output the put command will let you output the number. Yes, thank you. But I really don't understand how to incorporate them together. And it doesn't really explain in the guide how to use different colours in a loop body. I don't think we can use colour as a counter because it is a procedure. So I need some more elaboration on this. Thanks for your time. |
Author: | TheGuardian001 [ Mon Nov 15, 2010 9:20 pm ] |
Post subject: | Re: Loop situation |
redsp0t @ Mon Nov 15, 2010 9:00 pm wrote: TheGuardian001 @ Mon Nov 15, 2010 5:45 pm wrote: the color() method will let you set the text colour.
a for loop will let you do something 15 times, and gives you a variable to output the put command will let you output the number. I don't think we can use colour as a counter because it is a procedure You don't use the colour command as a counter, you use the for loop as a counter. create a for loop. inside of it, choose a colour (using the colour() method). now output a number. now end your loop. |
Author: | Insectoid [ Mon Nov 15, 2010 9:21 pm ] |
Post subject: | RE:Loop situation |
The color function takes an integer as a parameter. For loops pass an integer into the block. Got any ideas yet? |
Author: | TokenHerbz [ Mon Nov 15, 2010 10:40 pm ] |
Post subject: | RE:Loop situation |
use the for loop counter to change the colors. |
Author: | redsp0t [ Mon Nov 15, 2010 10:58 pm ] |
Post subject: | RE:Loop situation |
This is more what I was looking for, but thanks I solved my own question, which I think is better than getting the answer from someone else. Thank you all for taking the time to comment and try and help. const DELAY := 500 const REPETITION_LIMIT := 15 var integer : int integer := 1 loop colourback (integer) colour (0) put integer integer := integer + 1 delay (DELAY) exit when integer > REPETITION_LIMIT end loop |
Author: | DemonWasp [ Mon Nov 15, 2010 11:24 pm ] | ||
Post subject: | RE:Loop situation | ||
While it is true that it's best to build your answers than simply copy from others, you should know that what you've written there is a common pattern known as a counted loop. It is commonly solved by using for statements, though there are plenty of languages that use different means. Your code can be shrunk to the following
As you can see, the for construct removes the need for the following: - declaring the integer variable - incrementing the integer variable - checking and exiting when you hit the maximum |
Author: | TokenHerbz [ Tue Nov 16, 2010 2:52 am ] | ||
Post subject: | Re: Loop situation | ||
from the OP, this should suffice. //for statement for amount of numbers, using it to also change colors of the numbers that you put down.
|