Java
  Home arrow Java arrow Page 2 - Java Statements
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-10-24

    Table of Contents:
  • Java Statements
  • While Loop
  • Do-While
  • For Loop
  • To Be Continued...

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java Statements - While Loop


    (Page 2 of 5 )

    The first iteration we will look at is called the While Loop. It is your standard loop that can best be described as: while this is true, keep doing this.


    class CountDown {

    public static void main(String args[]) {

    int timer = 10; // Sets initial value of timer to 10


    while(timer > 0) { // Says do this while timer > 0

    System.out.println(“T minus “ + timer);

    timer--; // Decrements timer by -1 each loop

    }

    }

    }

    The above code is a timer, exactly like the one used by NASA. If you ran the program, the following would print to your screen:

      T minus 10

      T minus 9

      T minus 8

      T minus 7

      T minus 6

      T minus 5

      T minus 4

      T minus 3

      T minus 2

      T minus 1

    The code gives the variable timer an initial value of ten and then as it passes through each loop, it decrements the timer's value by one, prints the string “T minus" and appends the current value of timer to it. It does this until the value of timer is no longer greater than 0.

    Just as a side note, if the value of timer was not greater than 0 to begin with, the program would have skipped the loop altogether.

    More Java Articles
    More By James Payne


       · This article discusses Java Statements, like the If statement and Loops (such as the...
     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway