Week 4 Discussion

 

Please read carefully the questions that follow. Then think about them, do your research, write up your responses (on your own computer) to ONE of the sub-questions supporting it with examples, and post them into the current conference.

 

1.(a) What is the output of the following code segment?

 

int n = 3;

   while (n > 0) {

   System.out.print(n + " ");

   --n;

   int m = 3;

   do {

       System.out.print(m + " ");

       --m;

   } while (m > 0);

}

The output is:

The variable n is initialized to 3 and and the while loop is entered since 3 > 0.  The initial value of n is 3 which is outputted, then n is decremented.  The value of n is now 2 and the code continues to the do-while loop.

m is initialized to and set to 3 and the loop is entered.  The initial value of m is 3, which is printed on the same line as the first loop.  m is then decremented and now has a value of 2.  The do-while loop continues for another iteration and outputs 2.  m is set to 3 and the do-while loop iterates again and outputs 3 2 1 again.  This continues until n reaches zero and the code terminates.

Outer loop is purple and the inner loop is red.

Output: 3 3 2 1 2 3 2 1 1 3 2 1

 

Send me email

Visit my website

 

 

 

Home | Photos | Favorite Links | Matt's Bio

UMUC | Feedback | Guest book | E-mail me

 

FaulknerWeb.com is designed and managed by Matt Faulkner

Domain Name service provided by Namesecure

Web Service by eDataRack
Copyright © 1998 - 2009 [FaulknerWeb.com]. All rights reserved.
Updated: June 16, 2009 09:35 .