Looping Constructs

Many complex tasks require the repetition of an action. A loop is a way of repeating a statement until there is no way to end the loop. Consider a scenario that we have to print count from 1 to 10.

If we use sequential programming approaches, then we need to write ten statements to print every number 1 to 10:


System.out.println(1); 
System.out.println(2); 
…….. 
System.out.println(10); 

The above technique makes our program long and takes longer time to write code. The best way to solve the above problem is to use the loop. In a loop there are many elements to initialization, position, increase / decrease, and to repeat the verb.

The initial tells where to start from. This part of the loop is executed only once in the beginning of the loop. Explains the condition to end looping. In each iteration, this part of the loop is evaluated.

And the results of the loop (right or wrong) are executed on the basis. If the condition of the true body of the loop is evaluated, otherwise the control is out of the loop.

Growth / decrease tells the steps that should be taken to reach the situation. The body of the loop consists of statements which should be repeated.

Java provides three looping structures:

  • for
  • while
  • do…while

The for loop

For Loop provides a compact way to resume on a series of values. It is often called "for loop" because the way it loop repeatedly until a particular situation is satisfied.

Syntax:


for (initialization; termination; increment / decrement) {
	statement(s)
}

When using this version of the for statement, keep in mind that:

  • Initial expression launches the loop; After the loop starts, it is executed once.
  • When the expiry expression evaluates on the wrong, the loop is finished.
  • Increasing expression is called after each recurrence through the loop; It is perfectly acceptable to increase or decrease the value for the expression.


Examples:


for (int count = 1; count < = 10; count++) {
	System.out.print(count);
}

//Output: 
1 2 3 4 5 6 7 8 9 10

The while loop

The "while loop" is an alternative to "for loop". A "for loop" is usually used when the number of times the block is to be executed is known.

A "while loop" is usually used when the number of times the block is to be executed is not known, like prompting the user whether to stop executing during every iteration.

Syntax:


while (condition) 
{  
	Statement(s);  
	Increment/Decrement 
} 

Example:


int cout=1; 
while(count<=10) 
{  
	System.out.print (count);  
	count++; 
} 
//Output: 
1 2 3 4 5 6 7 8 9 10 

The do…while loop

In a while statement, Evaluation of expression is done at the beginning of the loop and the body of the loop can not be executed at any time if the first evaluation of logical expression is proved wrong.

Do-while Statement is similar to a while loop except that the body of the loop always executes at least once. Syntax:


do {
	Statement(s)
	Increment / Decrement
} while ( condition );

Example:


int count = 1;
do {
	System.out.print(count);
	Count++;
} while ( count < = 10 );

//Output: 
1 2 3 4 5 6 7 8 9 10  

Note that a loop body can have any statement, expression, a conditional construct if…else or switch…case and another looping statement as well. When a looping statement is put inside another looping statement it is called as nested loop.

The break and continue

The break and continue statements are the handles provided by java to control the loop execution.

The break :

The statement makes the control out of the loop. A brake is faced as a loop; As soon as the statement is executed, the remaining statements have been left and statements have been given. We use brakes; Whenever our condition matches, we say the statement and we want to end the loop. The break; of the beach; Our loop is used to make it more flexible and gives more power to make it more adaptable because it avoids unnecessary iterations.

For example: we have to find the first number which is divisible by 5 and 7 between 1 and 100;


int num = 1;
while (num < = 100) {
	if (num % 5 == 0 && num % 7 == 0) {
		System.out.println("Number is" + num);
		break;
	}
	i++;
}

In the above code break; statement would cause the loop to be terminated as the value of num is divisible by both 5 and 7 that is, 35. So the loop will not have unnecessary 65 iterations.

The continue :

Statement skips the rest of the statement in the body of the loop and continue with the next iteration of the loop.

For example we want to print all the numbers between 50 and 100 which are not divisible by 7.


int num = 50;
while (num) {
	if (num % 7 == 0) continue;
	System.out.println(num);
}

In the above code when the num is divisible by 7 continue; statement causes the loop to skip System.out.println(num) statement. That mean when the num is not divisible by 7, System.out.println(num) statement is executed.

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group