Welcome to the CSC Q&A, where you can get help (and share your knowledge) about computer science!

Can you make the generalization that typically the outer loop in a nested loop will dictate the number of lines?

+11 votes
asked Sep 15, 2016 in CSC 211 (F16) by Audrey Hogenkamp (100 points)

3 Answers

+3 votes

When you are writing a loop, it is useful to make the outer loop correspond with the number of lines you are printing. But remember, a loop tells how many times the code inside will be run. It is possible to have a loop that does not print anything but alters a variable by following an algorithm. It is also possible to have a loop that prints outputs all on the same line.

To answer the question: no, it is better not to generalize, but rather read each loop carefully to see what it does.

answered Sep 15, 2016 by Sara Estrada (100 points)
+2 votes
If you end the outer loop with a println statement, and that is your only println statement in the nested loop, the outer loop will dictate number of lines.
answered Sep 15, 2016 by Jack Cannell (100 points)
0 votes
Yes one way to think of a nested loop could be to think of it like a math table where the outer loop gives the rows of the table and the inner loop goes through the columns of the table in each row.
answered Sep 15, 2016 by Jared Haeme (100 points)
...