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

jGuru: Help: Using Java For-Loops

 

by jGuru

[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.

    Task 1

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

    The basic syntax for a for loop is:

    for(<initializer>;<loop test>; <increment>) {
       <loop body>
    }
    

    Task 2

    Define an integer sum, initializing it to 0.

    Define an "int" not an "Integer".

    Task 3

    Make a loop that counts from 1 to 6.

    See help for task 1. In the next task you will add a loop within the body of this loop.

    Task 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.

    To increment the variable sum, use the expression sum++

    Task 5

    Print out the value of sum.

    The basic code to print out a string is:

    System.out.println("Hello World");
    

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