Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with Loops
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Gadd




PostPosted: Sun Mar 25, 2012 12:59 pm   Post subject: Help with Loops

Hey guys, I wanted to do a bit more practice on my looping and wanted to write this program asks the user how many multiples of 2 the user wants to see, then display it on the screen. I do know how the loops and for loops work Im just wondering how I would work with getting 2,4,6,8 on the screen if the user says 4. Thanks for the help if possible!
Sponsor
Sponsor
Sponsor
sponsor
Yves




PostPosted: Sun Mar 25, 2012 1:34 pm   Post subject: Re: Help with Loops

This tutorial ought to help you.
You should use a for loop.
How about this: try it, post it, and if it still doesn't work, somebody can help you.[/url]
Gadd




PostPosted: Sun Mar 25, 2012 1:46 pm   Post subject: RE:Help with Loops

code:

var num_multiples : int
var multiple : int

multiple := 2
put "How many multiples of ", multiple, " would you like to see?"
get num_multiples

for i: multiple .. num_multiples by multiple
    put ""
    put"":20, i
    exit when num_multiples = 0
end for
put ""
put "The program has displayed, ", num_multiples, " multiples of ", multiple


Thanks for the help, I also added being able to change the multiple to whatever you want. Thanks.
Rickkwa




PostPosted: Mon Mar 26, 2012 6:40 pm   Post subject: Re: Help with Loops

Well, from that code, if num_multiples was 10,
it would print all the multiples from 2 to 10 => 2, 4, 6, 8, 10.
But I thought the program was to print 10 multiples of 2 => 2, 4, 6, 8, 10, 12, 14, 16, 18, 20

So I'd change the for loop to
code:

for i: 1..num_multiples
    put ""
    put "":20, i * multiple
end for


Also, in your for loop, exit when num_multiples = 0, doesn't have much meaning.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: