Wednesday, November 2, 2011

Challenge 029’s Solution

When the code is placed into BlueJ™, the following comes out in the Terminal window:
 
******
******
******
******
******
 
This occurs because of the for command—a statement similar to the while loop. It starts by setting k as an int value of 1. It then checks if k is ≤ 5, which it is. This leads the for loop to execute its own code. This code consists of a second for loop, which sets i as an int value of 1 and operates as long as i is ≤ 6. Because i is ≤ 6, its code, which is to print “*”, is done. Then, i’s for statement adds 1 to i and repeats as long as i is ≤ 6. This causes the character “*” to be printed six times before i’s for statement ends. The end of k’s code skips to the next line. After this, k’s for statement adds 1 to k and repeats as long as k is ≤ 5. This causes the line “******” to be printed five times before k’s for statement ends.

−Phoenix

1 comment:

  1. Solid explanation concerning the nested loops. Good work. 50 XPs awarded for completion.

    ReplyDelete