• Home
  • About
  • Work Portfolio
Blue Orange Green Pink Purple

Archive for the ‘Java’ Category

You can use the search form below to go through the content and find a specific post or page:

Jan 30

Java – do loop

The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once.


int num = 0;
do {
System.out.println("Line " + (num++));
} while (num < 5);

The loop will continue to increment by one on every loop untill it reaches 5.

Jan 30

Java – foreach equivalent

Java 5 introduced what is sometimes called a “for each” statement that accesses each successive element of an array, List, or Set without the bookkeeping associated with iterators or indexing.


String[] names = {"Michael Maus", "Mini Maus"};

for (String s : names) {
System.out.println(s);
}

the output results


Michael Maus
Mini Maus

Jan 30

Java – while loop

The while statement is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate.


int i = 1;
while (i <= 5) {
System.out.println(i + " squared is " + (i * i));
i++;
}

The output result for the above could would be

1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25

Jan 30

Java – for loop

Many loops consist of three operations surrounding the body: (1) initialization of a variable, (2) testing a condition, and (3) updating a value before the next iteration. The for loop groups these three common parts together into one statement, making it more readable and less error-prone than the equivalent while loop. For repeating code a known number of times, the for loop is the right choice.


for(int count = 0; count < 10; count++) {
System.out.println("Line " + (count));
}

The output result for the above could would be

Line 0
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9

Noormohamed

  • About
    About me. Edit this in the options panel.
  • Photo Stream
  • Categories
    • CSS
    • Donate
    • Hosting
    • Java
    • jQuery
    • PL/SQL
    • VMWare
    • WordPress Plugin
  • Recent Articles
    • PL/SQL Basic Insert (no return)
    • VMWare Player on a VMWare ESX Guest
    • Java – do loop
    • Java – foreach equivalent
    • Java – while loop
    • Java – for loop
  • Archives
    • February 2010
    • January 2010
    • April 1999
  • Search






  • Home
  • About
  • Work Portfolio

© Copyright Noormohamed. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top