Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Exercises
  


2. Using Java For-Loops: Help page 6 of 31




  • 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 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact