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

Username:   Password: 
 RegisterRegister   
 nested loops!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
cutiegirl_14_16




PostPosted: Sun Mar 02, 2003 4:58 am   Post subject: nested loops!

could somebody help me with nested loops ans for statements... I don't understand for statements, and I need it explained to me in laymans terms.

here's an example of 2 of the programs.

1. Write a program to evaluate xsqaured -ysquared for all combinations of x where x=-2,-1,0,1,2 and y=0,2,4,6,8,10. print out the results in table form.

2. given data which contains the number f children in a family followed by names of those children and en end of file record

3 john susan mark
1 michelle
4 alex david ryan chirs
3 megan adam lee
2 jesse justin
write a program to read the data from a file called childdat.t and print the names of the children in each family. Output the average number of children in the families.

THANX!
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Mar 02, 2003 11:04 pm   Post subject: (No subject)

loops keep on running over and over until an exit statment is given. In forloops, theres a build in counter that keeps track of how many times the loop was executed and when sertain number is reached, it will terminate.

In nested loops, the inside loop will run for each value of outside loop.

such as
code:

for x:1..3
for y:1..3
put x, " ", y
end for
end for


this will output
1,1
1,2
1,3
2,1
2,2
etc...

thats because inside forloop (1..3) will execute for each time outter loop is run so there will be a total of 6 outputs (3*3)

This technique is very useful to read/write grids/matrix systems.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
azndragon




PostPosted: Mon Mar 03, 2003 6:17 am   Post subject: (No subject)

For loops really help with probability programs. I had it calculate how many combinations are possible using 12 numbers, and have it calculate how many times certain numbers appeared in certain slots, like how many times the number 1 would be first on the list, etc.
cutiegirl_14_16




PostPosted: Thu Mar 06, 2003 8:21 pm   Post subject: thanx

thanx 4 the help!
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: