Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Using Java For Loops

 

by jGuru

[Help | API Docs | Short Course| Exercises]

For this exercise you will use a loop to print out some strings and use a nested loop to simulate a multiplication (6*3).

[If you are using IBM's VisualAge for Java for this exercise, then use the scrapbook to type and test your code (without a main method etc...), thus, avoiding the need to fill in the skeleton code, compile, and run from the command line.].

Prerequisites

  • None.

Skeleton Code

Tasks

  1. Make a for loop that prints "Hello" 5 times.

  2. Define an integer sum, initializing it to 0.

  3. Make a loop that counts from 1 to 6.

  4. Make an inner loop, nested within the previous loop, that counts from 1 to 3. The statement of the inner loop should increment sum.

  5. Print out the value of sum.

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

If you execute Forloop from a command line, the output should be similar to the following:

Hello
Hello
Hello
Hello
Hello
sum is 18

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.