Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Loops in Java
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
grasshopper




PostPosted: Tue Mar 23, 2004 11:22 pm   Post subject: Loops in Java

Does anyone know of a tutorial I can look up on either this site or another site about loops in java.. like for or while.. loops.. things like those.. Or does anyone want to try to explain it to me.. I`m lookig\ng for an example and then a step by step explantationn or something like that...

Thankssss
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Wed Mar 24, 2004 12:04 am   Post subject: (No subject)

Loops in Java work pretty much like loops in C. The folks at Sun who work on Java are all big C geeks. Smile

code:
// Let's loop three times and print the result each time
// Ignore the boilerplate code.

import java.lang.*;
import java.io.*;

class Test {
   public static void main(String[] args) {
      int i;
      for (i = 1; i <= 3; i++) {
         System.out.println("" + i);
      }
   }
}


Basically, though, any type of loop consists of three things.


  • First you have the initialization. In the above example I set the variable i to 1.
  • Second, you test to see if the loop should continue. In the above example, I did that by check if i is less than or equal to 3.
  • Third is the update. In this case, I incremented i by one.


Java, like C also has the while loop. Here we don't have the neat syntactic way of showing those three steps. Instead, the only thing that falls between parentheses is the test.

code:
import java.lang.*;
import java.io.*;

class Test {
   public static void main(String[] args) {
      int i = 1;
      while (i <= 3) {
         System.out.println("" + i);
         i++;
      }
   }
}


Here I've done my initialization of i before entering the loop, and the updating of i comes inside the body of the loop. This is a trivial example, and the power of the while loop is more readily visible when the loop involves initializing and updating several variables.

Finally, the do ... while loop. The primary reason for using this type of loop is that the test doesn't occur until after the first time the loop body has run. This might be advantageous in prompting for a user's input at least once, then testing the input, then only running the loop again if the input was unacceptable.

code:
import java.lang.*;
import java.io.*;

class Test {
   public static void main(String[] args) {
      int i = 1;
      do {
         System.out.println("" + i);
         i++;
      } while (i <= 3);
   }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: