Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (1 vote)
1K views

1-8-Iterative Constructs in Java

1. The document discusses different types of iterative constructs or loops in Java programming, including for, while, do-while, and nested loops. 2. Loops are divided into two categories: entry-controlled loops like for and while that check the loop condition first before entering the body, and exit-controlled loops like do-while that execute the body first before checking the condition. 3. The document provides examples of each loop type and also covers concepts like break, continue, and user-controlled loops. Interconversion between different loop types is also demonstrated.

Uploaded by

Ashwini KS
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
1K views

1-8-Iterative Constructs in Java

1. The document discusses different types of iterative constructs or loops in Java programming, including for, while, do-while, and nested loops. 2. Loops are divided into two categories: entry-controlled loops like for and while that check the loop condition first before entering the body, and exit-controlled loops like do-while that execute the body first before checking the condition. 3. The document provides examples of each loop type and also covers concepts like break, continue, and user-controlled loops. Interconversion between different loop types is also demonstrated.

Uploaded by

Ashwini KS
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

1

ITERATIVE CONSTRUCTS
IN JAVA
By,
Ashwini K S
Lorven Public School, Chandarapura
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 Exit controlled
Entry controlled loop
loop. loop

• Based on the flow of control, the looping


constructs can be categorised into two types.
Lorven Public School, Chandarapura
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

Lorven Public School, Chandarapura


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 :

Lorven Public School, Chandarapura


5

Lorven Public School, Chandarapura


6

EXAMPLE
for (i=0 ; i< 100 ; i++)
{
System.out.println(“ 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.
https://www.geeksforgeeks.org/java-for-loop-with-examples/

Lorven Public School, Chandarapura


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

Lorven Public School, Chandarapura


8

EXAMPLE

https://www.geeksforgeeks.org/java-while-loop-with-exam
ples
/
Lorven Public School, Chandarapura
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.

Lorven Public School, Chandarapura


10

Lorven Public School, Chandarapura


11

Lorven Public School, Chandarapura


12

Lorven Public School, Chandarapura


13

Lorven Public School, Chandarapura


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.

Lorven Public School, Chandarapura


15

Lorven Public School, Chandarapura


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.
• https://compiler.javatpoint.com/opr/test.jsp?filename=ContinueExample

Lorven Public School, Chandarapura


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

}
System.out.println(i);
}
}
Lorven Public School, Chandarapura }
18

INTERCONVERSION OF LOOPS

Lorven Public School, Chandarapura


19

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

Lorven Public School, Chandarapura


21

Lorven Public School, Chandarapura


22

USER CONTROLLED LOOPS


• Sometimes users make the decision about whether to continue the loop or not.
• Example:
Int i=4,j=1,p;
String m=“yes”;
while(m.equals(“yes”))
{
p=i*j;
System.out.println(p);
j++;
System.out.println(“Do you want to continue…..(yes/No)”);
m=in.readLine();
}
Lorven Public School, Chandarapura
FINITE LOOP 23

Lorven Public School, Chandarapura


24

INFINITE LOOP

Lorven Public School, Chandarapura


25

EMPTY/NULL LOOP

• Empty loops are used create delay in the execution of the program.
• They are also referred as delay loops.

Lorven Public School, Chandarapura


26

Lorven Public School, Chandarapura


27
// To check Niven number
import java.util.*;
class Sol40
{
static void main()
{
Scanner sc=new Scanner(System.in);
int d,s=0,num,I,n;
System.out.println(“Enter a number:”);
num=sc.nextInt();
n=num;
do
{
d=n%10;
s+=d;
n=n/10;
}
While(n!=0)
if(num%s==0)
System.out.println(“Niven Number”);
else
Lorven Public School, Chandarapura System.out.println(“Not a Niven Number”);
28

Lorven Public School, Chandarapura


29

Lorven Public School, Chandarapura


30

Lorven Public School, Chandarapura


31

Lorven Public School, Chandarapura


32

Lorven Public School, Chandarapura


33

Lorven Public School, Chandarapura


34

Lorven Public School, Chandarapura


35

Lorven Public School, Chandarapura


36

Lorven Public School, Chandarapura


37

Lorven Public School, Chandarapura


38

Lorven Public School, Chandarapura


39

Lorven Public School, Chandarapura


40

Lorven Public School, Chandarapura

You might also like