IT Community - Software Programming, Web Development and Technical Support

Javaa:Tutorial - The Loop

This is a discussion on Javaa:Tutorial - The Loop within the Java Programming forums, part of the Software Development category; As with all programming languages I know, there are two types of loops. The while loop and the for loop ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-24-2007, 12:58 AM
pranky pranky is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 51
pranky is on a distinguished road
Default Javaa:Tutorial - The Loop

As with all programming languages I know, there are two types of loops. The while loop and the for loop which, in all practicality, do the same exact thing; execute code while a condition is true. When I first started programming, I always preferred the while loop because I could never memorize the syntax of the for loop. However, now that I have memorized the proper syntax it is usually the only type of loop I use, because it is more concise, which is a very good programming tactic to follow. If you can write a piece of code in three lines rather than five, it is easier to reed.

The basic structure of a loop is as follows:
Code:
Code:

while(true) {
//evaluate the code
}
A loop that actually does something might look like this.
Code:
Code:

int x = 0;
while(x < 5){
System.out.println(x);
x++;
}
Java also as a do…while loop. The main difference between the while loop and the do…while loop is that the do…while loop always runs at least once. For example take a look at the code below
Code:
Code:

 int x = 0;
do {
System.out.println(x);
x++;
} while (x >= 5);
The variable x is never greater than or equal to five, but it still prints the initial value of x, 0. However, although looking at the while loops, each line should make perfect sence, there are two lines of unnecessary code, and while we strive to be “good” programmers, we frown upon that; which is why there is a for loop. The for loop looks like this

Code:
Code:

for(int x = 0; x < 5, x++){
System.out.println(x);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop in Mysql Murali Database Support 0 02-12-2008 06:37 AM
Pro*C Tutorial Sabari C and C++ Programming 0 09-11-2007 04:53 AM
How can i do loop testing manually? sundarraja Software Testing 1 07-25-2007 01:02 AM
What are the best tutorial sources? SageMother HTML, CSS and Javascript Coding Techniques 3 03-05-2007 10:37 AM
PHP:Tutorial - Getting to know GD pranky PHP Programming 0 02-23-2007 09:56 AM


All times are GMT -7. The time now is 10:59 PM.


Copyright 2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0