Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Tutorials > Java Tutorials

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Tutorials Tutorials and Code for Java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-09-2006, 10:01 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 3,205
Last Blog:
Passwords
Credits: 842
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John
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:
while(true) {
//evaluate the code
}
A loop that actually does something might look like this.

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:
 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:
for(int x = 0; x < 5, x++){
System.out.println(x);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB 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
Javascript: Loop through ids dirkfirst JavaScript and CSS 1 07-02-2007 02:28 PM
Sounds and Sound Loop Resources TVDinner Website Design 0 03-10-2007 08:20 AM
Will loop ever terminate??? kphilux General Programming 8 03-07-2007 09:55 AM


All times are GMT -5. The time now is 05:01 AM.

Contest Stats

Xav ........ 1024.41
MeTh0Dz|Reb0rn ........ 974.08
morefood2001 ........ 850.04
John ........ 841.93
WingedPanther ........ 661.52
marwex89 ........ 575.59
Brandon W ........ 456.18
chili5 ........ 292.12
orjan ........ 187.41
Steve.L ........ 181.88

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 79%

Ads