0% found this document useful (0 votes)
15 views39 pages

Java Iterative Constructs Explained

Uploaded by

anupama.sudheer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views39 pages

Java Iterative Constructs Explained

Uploaded by

anupama.sudheer
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

ITERATIVE CONSTRUCTS
IN JAVA
2

INTRODUCTION

• Executing a set of statements repeatedly


until the given task is completed is known as
iterative construct or LOOP.
Looping constructs

• Iteration means repeated execution of set of


statements. This can be achieved by using
loop. Entry controlled Exit controlled
loop loop

• Based on the flow of control, the looping


constructs can be categorised into two types.
3

ENTRY CONTROLLED LOOPS


• A looping construct in which the condition is checked in the beginning is entry
controlled loop.

• In this, if the condition is true the control is allowed to enter into the loop otherwise
the entry will be denied.

• There are two types of looping constructs in this category:


• for loop
• while loop
4

‘FOR’ LOOP

• This loop is used when the number of iterations are fixed.


• It is fixed iteration looping construct.
• Components of ‘for loop structure are :
5
6

EXAMPLE
for (i=0 ; i< 100 ; i++)
{
[Link](“ Welcome to Java”);
}
✓This loop repeats for 100 times i.e., from 0 to 99.
✓Every the loop gets executed the statement welcome to Java is printed.
✓When i becomes 100 then the condition fails and control comes out of the loop.
7

‘WHILE’ LOOP

• This loop is used when the number of


iterations are not fixed.

• In this , the block of code gets executed till


the given condition becomes false.

• While loop is unfixed looping construct.

• Syntax of while loop is


8

EXAMPLE
9

EXIT CONTROLLED LOOPS

• An exit control loop, controls exit of the loop, that's why it is referred to as exit
control loop.
• An exit control loop checks the condition for exit and if given condition for exit
evaluate to FALSE, control will exit from the loop body or else control will enter
again into the loop.
• In this loop body is executed first and then the given condition is checked
afterwards.
• An example of exit controlled loop is Do While Loop.
10
11
12
13
14

BREAK STATEMENT
• When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the
loop.
• The Java break statement is used to break loop or switch statement. It breaks the
current flow of the program at specified condition. In case of inner loop, it breaks
only inner loop.
• We can use Java break statement in all types of loops such as for loop, while
loop and do-while loop.
15
16

CONTINUE STATEMENT

• The continue statement is used in loop control structure when you need to jump to
the next iteration of the loop immediately.
• The Java continue statement is used to continue the loop.
• It continues the current flow of the program and skips the remaining code at the
specified condition.
• In case of an inner loop, it continues the inner loop only.
• We can use Java continue statement in all types of loops such as for loop, while loop
and do-while loop.
17

//Java Program to demonstrate the use of continue statement


//inside the for loop.

public class ContinueExample


{
public static void main(String[] args)
{
//for loop
for(int i=1;i<=10;i++)
{
if(i==5)
{
continue; //using continue statement it will skip the rest statements

}
[Link](i);
}
}
}
18

INTERCONVERSION OF LOOPS

Lorven Public School, Chandarapura


19

i=1; i=1;
for(i=1; i<p ; i++)
do
{ While(i<p)
{
if (a %i==0 &&b%i==0) { if(a% i==0&&b%i==0)
gcd=i; if(a% i==0&&b%i==0) gcd=i;
} gcd=i; i++;
i++; }
} While(i<p);
20

Lorven Public School, Chandarapura


21
FINITE LOOP 22

Lorven Public School, Chandarapura


23

INFINITE LOOP
24

EMPTY/NULL LOOP

• Empty loops are used create delay in the execution of the program.
• They are also referred as delay loops.
25
26
// To check Niven number
import [Link].*;
class Sol40
{
static void main()
{
Scanner sc=new Scanner([Link]);
int d,s=0,num,I,n;
[Link](“Enter a number:”);
num=[Link]();
n=num;
do
{
d=n%10;
s+=d;
n=n/10;
}
While(n!=0)
if(num%s==0)
[Link](“Niven Number”);
else
[Link](“Not a Niven Number”);
27
28
29
30

Lorven Public School, Chandarapura


31
32
33
34
35
36
37
38

Lorven Public School, Chandarapura


39

Lorven Public School, Chandarapura

Common questions

Powered by AI

A 'for' loop is suitable when the number of iterations is known before entering the loop, allowing for initialization, condition check, and increment/decrement to be handled in a concise manner. Conversely, a 'while' loop is ideal when the number of iterations is not predetermined and depends on a condition evaluated at each iteration .

Proper management of loop control structures is crucial in software development to ensure efficiency and correctness. Incorrect use can lead to logical errors, infinite loops, or improper resource utilization, negatively impacting performance and stability. Structured use enables clear and concise code logic, enhances troubleshooting and scalability, and prevents common pitfalls associated with unoptimized loops .

The 'continue' statement is utilized within loops to skip the remaining statements in the current iteration and jump to the next iteration's evaluation. It's particularly useful when certain conditions should result in bypassing specific logic inside the loop while still continuing the loop process. In nested loops, it applies to the innermost loop .

The 'break' statement in Java is used to exit a loop prematurely when a certain condition is met. It immediately terminates the loop and transfers control to the statement following the loop. This is useful in all loop types, such as 'for', 'while', and 'do-while', and can be employed within nested loops to exit only the innermost loop .

The 'do-while' loop is advantageous for input validation as it ensures the loop body executes at least once, allowing initial processing and feedback to the user before the validity of the input is checked. This guarantees that the prompt or action occurs irrespective of the initial condition, which is ideal when needing at least one interaction, such as asking for correct input again .

An entry controlled loop evaluates the condition before executing the loop body, meaning the loop may not execute at all if the condition is initially false. Examples include the "for" and "while" loops. In contrast, an exit controlled loop like the "do-while" loop executes the loop body first before checking the exit condition, ensuring that the loop runs at least once .

To convert a 'for' loop into a 'while' loop, initialize the loop variable before the 'while' statement, use the loop's condition as the 'while' condition, and increment/decrement inside the loop body. For example, the 'for' loop 'for(i=0; i<10; i++)' can be rewritten as: 'i = 0; while(i<10) { // loop body i++; }'. This maintains the loop's logic and behavior .

An empty/null loop, also known as a delay loop, serves the purpose of introducing a pause or delay during a program's execution. It has no statements within its body and is typically used to control the timing of application behavior when explicit waiting is necessary. This can be useful for timing control in applications lacking sleep functions .

Converting between different loop types can impact readability and maintainability. 'For' loops provide a compact structure suited for iterations with known bounds, enhancing clarity in such contexts. 'While' loops offer more flexibility but may complicate readability when not straightforward. Loop conversions must ensure logical equivalence and preserve the iteration semantics, demanding careful restructuring to avoid introducing errors .

Finite loops have a condition that eventually becomes false, terminating the loop after a certain number of iterations. Infinite loops, on the other hand, lack a condition that will ever evaluate to false, causing the loop to execute indefinitely unless externally interrupted. Infinite loops are often unintended due to logical errors, leading to performance issues and resource exhaustion if not controlled .

You might also like