Tuesday 26 July 2022

Iterative / Looping Statements in Java

Java’s iteration statements are for, while, and do-while. These statements create what we commonly call loops. As you probably know, a loop repeatedly executes the same set of instructions until a termination condition is met. As you will see, Java has a loop to fit any programming need.

1. while loop:

  The while loop is a pre-test loop. In this types of looping statement the condition is checked before executing the body of the loop. The syntax of while loop is 

While(condition)

{

      // body of the loop

}

The condition can be any Boolean expression.

The body of the loop will be executed as long as the conditional expression is true.

When condition becomes false, control passes to the next line of code immediately following the loop.

The curly braces are unnecessary if only a single statement is being repeated.

The following program show the working of do-while loop

class WhileDemo 
{
	public static void main(String[] args) 
	{
		int x=1;
		while(x<=10)
		{
		System.out.println(x);
		x++;
		}
	}
}

The body of the while (or any other of Java’s loops) can be empty. This is because a null statement (one that consists only of a semicolon) is syntactically valid in Java 



 

 

 

 

 

2. do-while loop

In the conditional expression controlling a while loop is initially false, then the body of the loop will not be executed at all.

However, sometimes it is desirable to execute the body of a loop at least once, even if the conditional expression is false to begin with. In other words, there are times when you would like to test the termination expression at the end of the loop rather than at the beginning.

Fortunately, Java supplies a loop that does just that: the do-while. The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.

The do-while loop is especially useful when you process a menu selection, because you will usually want the body of a menu loop to execute at least once.

The do-while is a post test loop where body of the loop executed before test condition. The syntax of the d-while loop is 

do

{

// body of the loop

}while(condition)

The following program show the working of do-while loop

class DowhileDemo
{
	public static void main(String[] args) 
	{
		int x=1;
		do
		{
			System.out.println(x);
			x++;
		}
		while (x<=10);	
	}
}


 Output:


 

 

 

 

 

 

 

3. For loop

In java for loop comes in variations. The traditional for loop like other languages C/C++ and with JDK 5.0 release java peoples added for-each enhanced loop to the language library. Both of these loops are more useful and you can find below how to use these in your java programs 

a) The traditional for loop

Like other languages java supports for loop to execute a group of statements as long as condition is true. The syntax of the for loop is 

for(initialization; condition;iteration)

{

//body of the loop

}

The for loop operates as follows:

  1. When the loop first starts, the initialization portion of the loop is executed. Generally, this is an expression that sets the value of the loop control
  2. variable, which acts as a counter that controls the loop. The important thing is here the initialization expression is only executed once.
  3. Next, condition is evaluated. This must be a Boolean expression. It usually tests the loop control variable against a target value.
  4. If this expression is true, then the body of the loop is executed. If it is false, the loop terminates.
  5. Next, the iteration portion of the loop is executed. This is usually an expression that increments
  6. or decrements the loop control variable.
  7. The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false.

class ForDemo 
{
	public static void main(String[] args) 
	{
		for(int i=0;i<10;i++)
		{
		
		System.out.println("Hello World!");
		}
	}
}

Output:


 

 

 

 

 

 

 

b) For each loop

The for for-each loop is specifically designed to handle the elements of a collection. Collections represent a group of elements.

For example, we can take array as a collection or any class in java.util package can be considered as a collection.

A for-each style loop is designed to cycle through a collection of objects, such as an array, in strictly
sequential fashion, from start to finish.

The advantage of this approach is that no new keyword is required, and no preexisting code is broken. The for-each style of for is also referred to as the enhanced for loop. The general form of the for-each version of the for is shown here:

for( type itr-var: collection)

{

  // statements

}

Here, type specifies the type and itr-var specifies the name of an iteration variable that will receive the elements from a collection, one at a time, from beginning to end. The collection being cycled through is specified by collection.

class ForEach 
	{
		public static void main(String args[]) 
			{
			int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			int sum = 0;
			// use for-each style for to display and sum the values
			for(int x : nums) 
				{
				System.out.println("Value is: " + x);
				sum += x;
				}
				System.out.println("Summation: " + sum);
			}
	}


 Output:

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top