Skip to the content

onlineexamguide

  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engineering Study Materials
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • Aptitude Tricks
    • Computer Knowledge
    • Logical Reasoning Tricks
    • Networking
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Java Loops

Java Loops

Table of Contents

  • Java Loops
    • For Loop
    • For loop Parameters:
    • for-each Loop
    • Example:
  • While Loop
    • Data-flow-diagram of While Block
    • Example:
  • do-while loop

Java Loops

Java Loops

The programming concept of a loop, which enables iteration over a series of statements, is crucial. The purpose of a loop is to execute a specific code block until the specified condition is met or until all elements of a collection (such as an array, list, or other collection) have been traversed. Loops are most frequently used to carry out repetitive tasks. For instance, we must write the print statement 10 times if we want to print a table of numbers. However, by using a loop, we can achieve the same result with a single print statement. The purpose of a loop is to keep running its block until the desired condition is met.

Java provides mainly three JAVA loops based on the loop structure.

  1. for loop
  2. while loop
  3. do while loop

We will explain each loop individually in details in the below.

For Loop

The for loop is used for executing a part of the program repeatedly. When the number of execution is fixed then it is suggested to use for loop. For loop can be categories into two type.

  1. for loop
  2. for-each loop

For Loop Syntax:

Following is the syntax for declaring for loop in Java.


for(initialization;condition;increment/decrement)
{  
//statement 
}  	
	

For loop Parameters:

To create a for loop, we need to set the following parameters.

1) Initialization

It is the initial part, where we set initial value for the loop. And It is executed only once at the starting of loop. It is optional, if we don’t want to set initial value.

2) Condition

It is used to test a condition each time while executing. The execution continues until the condition is false. It is optional and if we don’t specify, loop will be infinite.

3) Statement

It is loop body and executed every time until the condition is false.

4) Increment/Decrement

It is used for set increment or decrement value for the loop.

Data Flow Diagram of for loop

This flow diagram shows flow of the loop. Here we can understand flow of the loop.

Data Flow Diagram of for loop

For loop Example

In this example, initial value of loop is set to 1 and incrementing it by 1 till the condition is true and executes 10 times.


public class ForDemo1
{  
public static void main(String[] args) 
	{  
		int n, i;
		n=2;
		for(i=1;i<=10;i++)
		{  
			System.out.println(n+"*"+i+"="+n*i);  
		}  
}  	
}  
	

Example for Nested for loop

Loop can be nested, loop created inside another loop is called nested loop. Sometimes based on the requirement, we have to create nested loop.

Generally, nested loops are used to iterate tabular data.


public class ForDemo2
{  
public static void main(String[] args) 
{  
for(inti=1;i<=5;i++)
{  
for(int j=1;j<=i;j++)
{  
				System.out.print("* ");  
}  
System.out.println(); 
}  
}  
}  
	

for-each Loop

In Java, for each loop is used for traversing array or collection elements. In this loop, there is no need for increment or decrement operator.

For-each loop syntax

Following is the syntax to declare for-each loop in the Java.


for(Type var:array)
{  
//code for execution
}  
	

Example:

In this example, we are traversing array elements using the for-each loop. For-each loop terminates automatically when no element is left in the array object.


public class ForEachDemo1
{  
public static void main(String[] args) 
		{    
			inta[]={20,21,22,23,24};   
			for(int i:a)
{  
				System.out.println(i);  
			}  
}  
}  
	

While Loop

Like for loop, while loop is also used to execute code repeatedly. a control statement. It is used for iterating a part of the program several times. When the number of iteration is not fixed then while loop is used.

Syntax:


while(condition)
{  
//code for execution
}  
	

Data-flow-diagram of While Block

Data-flow-diagram of While Block

Example:

In this example, we are using while loop to print 1 to 10 values. In first step, we set conditional variable then test the condition and if condition is true execute the loop body and increment the variable by 1.


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

Example for infinite while loop

A while loop which conditional expression always returns true is called infinite while loop. We can also create infinite loop by passing true literal in the loop.

Be careful, when creating infinite loop because it can issue memory overflow problem.


public class WhileDemo2 
{  
	public static void main(String[] args) 
	{  
		while(true)
		{  
		System.out.println("infinitive while loop");  
		}  
	}  
}  
	

do-while loop

In Java, the do-while loop is used to execute statements again and again. This loop executes at least once because the loop is executed before the condition is checked. It means loop condition evaluates after executing of loop body.

The main difference between while and do-while loop is, in do while loop condition evaluates after executing the loop.

Syntax:

Following is the syntax to declare do-while loop in Java.


do
{  
//code for execution 
}
while(condition);  
	

Data Flow Diagram of do-while Block

Data Flow Diagram of do-while Block

Example:

In this example, we are printing values from 1 to 10 by using the do while loop.


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

Example for infinite do-while loop

Like infinite while loop, we can create infinite do while loop as well. To create an infinite do while loop just pass the condition that always remains true.


public class DoWhileDemo2 
{  
public static void main(String[] args) 
{  
        do
{  
System.out.println("infinitive do while loop");  
}while(true);  
}  
} 
	

Write a comment Cancel reply

You must be logged in to post a comment.

*
  • प्रवासन के लिए अंतर्राष्ट्रीय संगठन की पहली महिला महानिदेशक एमी पोप (Amy Pope) किस देश से हैं?

  • उत्तर – अमेरिका

  • किस केंद्रीय मंत्रालय ने ‘मेरी LiFE, मेरा स्वच्छ शहर’ अभियान शुरू किया?

  • उत्तर – आवास और शहरी मामलों के मंत्रालय

  • किस देश में अत्यधिक रोगजनक एवियन इन्फ्लुएंजा (Highly Pathogenic Avian Influenza – HPAI) की पुष्टि हुई है?

  • उत्तर – ब्राजील

  • किस संस्था ने ‘राष्ट्रीय ऊर्जा प्रबंधन केंद्र’ (National Energy Management Centre) की स्थापना की है?

  • उत्तर – REMC लिमिटेड

  • किस संस्था ने ‘World Tourism Barometer’ रिपोर्ट जारी की?

  • उत्तर – UNWTO

  • ‘साइबर सुरक्षित भारत’ किस संस्था/केंद्रीय मंत्रालय की पहल है?

  • उत्तर – इलेक्ट्रॉनिक्स और आईटी मंत्रालय

  • हाल ही में ख़बरों में रहा ‘प्रोजेक्ट-स्मार्ट’ किस केंद्रीय मंत्रालय से संबंधित है?

  • उत्तर – शहरी मामले मंत्रालय-रेलवे मंत्रालय

  • किस राज्य/केन्द्र शासित प्रदेश ने ‘जगन्नानकु चेबुदम कार्यक्रम’ (Jaganannaku Chebudam Programme) का शुभारंभ किया?

  • उत्तर – आंध्र प्रदेश

  • किस देश द्वारा प्रथम चीन-मध्य एशिया शिखर सम्मेलन (China-Central Asia Summit) की मेजबानी की जाएगी?

  • उत्तर – चीन

  • किस संस्था ने ‘Race to Net Zero’ शीर्षक से एक रिपोर्ट जारी की?

  • उत्तर – UN ESCAP

Recent Posts

  • CONSTRUCTION OF Cables & Selection
  • Ferranti Effect in transmission line
  • Insulator
  • String Efficiency
  • Corona Effect in Overhead Transmission Line
  • Types of Conductor
  • Proximity Effect
  • Skin Effect
  • What is GMD and GMR in Transmission Lines
  • Synchronous Reluctance Motor
  • Pass by Value and Pass by Reference in Java
  • JAVA Methods
  • BLDC Motor
  • OOPS Concepts in Java
  • Command line argument in JAVA

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming
  • C programming
  • C++ programming
  • Aptitude Tricks

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2023
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2023
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2023 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.