Tuesday, 19 February 2019

Looping in Java

1.What are Loops in Java

Java is Multi-featured language, loops in Java are a way to run a particular instruction or java function a number of times if, this is done after every iteration.
Java has 3 ways of executing the loops, they are all nearly similar, with some syntax and condition checking differences.

2. Types of Java Loops

a. While loop in Java

In Java, while loop, a loop is continuously executed if the Boolean condition comes true, it can be considered as a repeating if statement.
Loops in Java - While loop
Loops in Java – While loop
The syntax of Java While Loop
  1. while (boolean condition)
  2. {
  3. loop statements...
  4. }
While loop is an entry controlled loop, i.e. the condition is tested before the loop.
The condition is tested and the iteration follows until the condition becomes false, and that marks the end of the loop.
Example
  1. // Java program to illustrate while loop
  2. class whileLoopDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. int x = 1;
  7. // Exit when x becomes greater than 4
  8. while (x <= 4)
  9. {
  10. System.out.println("Value of x:" + x);
  11. //increment the value of x for next iteration
  12. x++;
  13. }
  14. }
  15. }
Output 
Value of x:1
Value of x:2
Value of x:3
Value of x:4

b. For loop in Java

Java for loop is a concise version of while loop, it provides the user to write the whole condition, i.e. initialization, condition and Increment/decrement in one line.
Syntax of Java for loop
  1. for (initialization condition; testing condition;
  2. increment/decrement)
  3. {
  4. statement(s)
  5. }
A for loop in java is also an entry controlled loop, we first initialize the variables in use and then the condition is tested, and accordingly, increment or decrement takes place.
Loops in Java - For Loop
Loops in Java – For Loop
Example 
  1. // Java program to illustrate for loop
  2. class forLoopDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. // for loop begins when x=2
  7. // and runs till x <=4
  8. for (int x = 2; x <= 4; x++)
  9. System.out.println("Value of x:" + x);
  10. }
  11. }
Output 
Value of x:2
Value of x:3
Value of x:4
Enhanced for loop
This version was introduced in Java 5, this feature allows you to iterate with elements of arrays. This feature should only be used when the index is unknown.
Syntax 
  1. for (T element:Collection obj/array)
  2. {
  3. statement(s)
  4. }
Example of enhanced for loop in Java
  1. // Java program to illustrate enhanced for loop
  2. public class enhancedforloop
  3. {
  4. public static void main(String args[])
  5. {
  6. String array[] = {"Ron", "Harry", "Hermoine"};
  7. //enhanced for loop
  8. for (String x:array)
  9. {
  10. System.out.println(x);
  11. }
  12. /*for loop for same function
  13. for (int i = 0; i < array.length; i++)
  14. {
  15. System.out.println(array[i]);
  16. }
  17. */
  18. }
  19. }
Output 
Ron
Harry
Hermoine

c. do while loop in Java

A do while statement is an exit controlled loop, i.e. it checks the condition after the execution of the loop.
Syntax
  1. do
  2. {
  3. statements..
  4. }
  5. while (condition);
Loops in Java - Do While Loop
Loops in Java – Do While Loop
Example of do while loop  in Java
  1. // Java program to illustrate do-while loop
  2. class dowhileloopDemo
  3. {
  4. public static void main(String args[])
  5. {
  6. int x = 21;
  7. do
  8. {
  9. //The line while be printer even
  10. //if the condition is false
  11. System.out.println("Value of x:" + x);
  12. x++;
  13. }
  14. while (x < 20);
  15. }
Output
Value of x: 21

3. Pitfalls in Loops in Java

a. Infinite loop

This loop takes place when the condition for some reason fails.
Example
  1. //Java program to illustrate various pitfalls.
  2. public class LooppitfallsDemo
  3. {
  4. public static void main(String[] args)
  5. {
  6. // infinite loop because condition is not apt
  7. // condition should have been i>0.
  8. for (int i = 5; i != 0; i -= 2)
  9. {
  10. System.out.println(i);
  11. }
  12. int x = 5;
  13. // infinite loop because update statement
  14. // is not provided.
  15. while (x == 5)
  16. {
  17. System.out.println("fail");
  18. }
  19. }
  20. }

b. Shortage of memory

This happens when there is a continuous addition in the loop but there occurs a shortage of memory.
Example 
  1. //Java program for out of memory exception.
  2. import java.util.ArrayList;
  3. public class Integer1
  4. {
  5. public static void main(String[] args)
  6. {
  7. ArrayList<Integer> ar = new ArrayList<>();
  8. for (int i = 0; i < Integer.MAX_VALUE; i++)
  9. {
  10. ar.add(i);
  11. }
  12. }
  13. }
Output 
Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.grow(Unknown Source)
at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at article.Integer1.main(Integer1.java:9)
So, this was all about Loops in Java. Hope you like our explanation.

No comments:

Post a Comment

Unity Top Download

Latest post

An Introduction to Hybris from basics

An Introduction to Hybris from basics:  -- ecommerce site and PCM(Product content Management) solutions. eg. croma website.  -- having sear...

Popular posts