Core Java Tutorials - Iterative Statements (Loops) in Java, For Each Loop in Java, For Loop, While Loop, Do-while Loop in Java


Iterative Statements (Loops) in Java, For Each Loop in Java, For Loop, While Loop, Do-while Loop in Java


Suppose that you want to print a string like "Java is an Object Oriented Programming" a 100 times. It would be lengthy process to write the following statement a 100 times:

System.out.println("Java is an Object Oriented Programming!");
System.out.println("Java is an Object Oriented Programming!");
...
...
...
System.out.println("Java is an Object Oriented Programming!");

So, how do you solve this problem?

Java gives a powerful feature called a loop that controls how many times an operation or a sequence of operations is performed in succession. Using a loop statement, you simply tell the computer to print a string a 100 times without printing the hard coded code to print the statement a 100 times, as follows: 

int c = 0;
while (c < 100) {

System.out.println("Java is an Object Oriented Programming!");

c++;
}


The variable c is initially 0. The loop checks whether (c < 100) is true. If so, it executes the loop body to print the message "Java is an Object Oriented Programming!" and increments c by 1. It repeatedly executes the loop body until (c < 100) becomes false. When (c < 100) is false (i.e., when c reaches 100), the loop terminates and the next statement. after the loop statement is executed.


Loops are constructs that control repeated executions of a block of statements. The concept of looping is fundamental to programming. Java provides three types of loop statements: while loops, do-while loopsfor loops and for-each loop.


The while Loop

The syntax for the while loop is as follows:

while (condition) 
{
// Loop body
Statement 1;
Statement 2;
........
........
Statement n;
}




Example

class whileDemo
{
public static void main(String st[])
{
char ch;
ch='a';
while(ch<='z'){
System.out.println(ch);
ch++;
}
}
}



The do-while Loop
The do-while loop is an another way of the while loop. But, The do-while loop executes the loop body first, then checks the loop condition to determine whether to continue or terminate the loop. The do-while loop must be execute at least one time whether the condition will true or false.The syntax is given below:

do {
// Loop body;
Statement(1);
Statement(2);
...........
...........
Statement(n);
} while (loop-condition);



Example:-
public class Test
{
public static void main(String st[])
{
int a=5;
int b=1;
do{
int c=1;
do{
if((c==b)||(c==a+1-b))
System.out.print("#");
else
System.out.print("#");
b++;
}while(c<=a);
System.out.println();
b++;
}while(b<=a);
}
}




The for Loop:-

A for loop can be used to simplify the proceeding loop:

for (i = initial Value; i < end Value; increments or decrements) {
// Loop body
Statement(1);
Statement(1);
........
........
Statement(n);
}

In general, the syntax of a for loop is as shown below:

for (initial-action; loop-condition;
action-after-each-iteration) {
// Loop body;

Statement(1);
Statement(1);
........
........

Statement(n);
}
Example:-

public class ForLoopsDemo
{
public static void main(String st[])
{
int loopVal;
int endVal=11;
for(loopVal=0;loopVal<endVal;loopVal++)
{
System.out.println("Loop Value "+loopVal);
}
}
}




For-Each Loop:-

The for-each loop is used to iterate a collection for example if we want to print the all elements of an array then we need to find the length of the array and iterate it from 0 to less then length but if we use the for-each loop we don't need to find the length of the array.

Example:-

int numbers[]=new numbers[]{12,23,43,25,65,67};
for(int num:numbers)
{
System.out.println(num);
}

it will print
12
23
43
25
65
67





{ 0 comments... read them below or add one }

Post a Comment