Tuesday, March 25, 2008

Boxes of Stars (Loops continued)

We learned how we could use loops to print out stars in various patterns like a box of stars [code]:



Or even an empty box of stars :



We have to use a Nested For Loop in order to print out lines of stars row by row. The outer loop represents each row, and the inner loop represents each column. Each row was constructed by using a String, since we can only print to a listbox one line at a time.

In the empty stars example we have to use If/Else statements in order to tell if we were printing out one of the borders. If we were at a border (outer or inner control variable is equal to 1 or num, where num is the total number of rows and columns) then we would print out a star, Else we will only print out a space.

Your HW is to print out the empty box of stars. You must ask the user if what number they like and then check to make sure that the number is in a range of 3 to 20.

No comments: