Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Theory

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Loops

Definition: A loop is a sequence of instructions that is continually repeated until a certain


condition is reached.

The following parameters are commonly used in a for loop:

1. An initial value for the loop control variable.


2. A condition—loop will iterate as long as this condition remains true.
3. An update expression to modify the loop control variable after every iteration.
4. The body of the loop consists of the statements that needs to be repeatedly executed

There are 2 types of loops.

1). Entry Controlled loop

a)while loop

b)for loop

2). Exit Controlled loop

a) do-while loop

Entry Controlled loop Exit Controlled loop

The test condition is evaluated at the beginning of The test condition is evaluated at the end of the
the loop. loop.

If the test condition is false the loop does not As the test condition is evaluated at the end, the
execute. body of the loop is always executed at least once,
even if the condition is false.

Example: For loop, while loop Example: do - while loop

Entry Controlled loop

2 types

a) Fixed Iterative loop


b) Unfixed Iterative loop
Fixed Iterative loop Unfixed Iterative loop

Allows a statement to be repeated a fixed The loop remains an indefinite number of


number of times according to the control iterations until some condition is met.
variable.

So we must know in advance how many The loop terminates defending upon conditions
repetitions may be needed. involving boolean flags.

Also called a definite loop Also called an indefinite loop


Example: for loop Example : while loop, do… while loop

1). While loop:

While loop is an entry control loop.

Syntax:

while(condition)

Body of the loop;

The while loop first evaluates the condition.

If the condition is true then body of loop will be executed. After execution of the body the
loop once again it checks the condition and if it true the statement with in the body are
executed once again. This process continues until the condition becomes false.

E.g.:-

In the example below, the code in the loop will run, over and over again, as long as a variable (i) is less than 5:
Output:

Note: Do not forget to increase the variable used in the condition, otherwise the loop
will never end!

for loop:-

For loop is an entry controlled loop.

Syntax:-

for(initialization-part ; condition ; inc/dcr-part)

Body of the loop;

When you know exactly how many times you want to loop through a block of code, use the for loop
instead of a while loop:

· initialisation is executed (one time) before the execution of the code block.

· Condition defines the condition for executing the code block.

· Inc/ dec part is executed (every time) after the code block has been executed.
● When the control is given to the for loop the initialization-part is performed first and
once.
● Then the condition is evaluated, if it is true all the statements present in the body of the
loop are exacted followed by the increment decrement part.
● After execution of inc/dec-part once again the condition is evaluated, if it is true once
again body of the loop and increment/decrement part are executed.
● This process continues until given condition is false

The example below will print the numbers 0 to 4:

Example explained

· initialisation sets a variable before the loop starts (int i = 0).

· Condition defines the condition for the loop to run (i must be less than 5). If the
condition is true, the loop will start over again, if it is false, the loop will end.

· Increment increases a value (i++) each time the code block in the loop has been
executed.

a) do-while loop

do-while is an exit control loop. In do-while the body of the loop will execute before testing
the condition. So the body of loop will execute at least once even though the condition is
false.

Syntax:-
do

Code statement;

while(condition);

§ The do-while loop directly enters into the Code statement and executes
the statements present with in the loop.

· After executing last statement the condition is evaluated . if the condition is true body of the
loop will be executed once again. This process continues until the condition becomes false.

· The loop will always be executed at least once, even if the condition is false, because the code
block is executed before the condition is tested:

for while

Suitable choice when we know the number of Helpful in situations where numbers of iterations
iterations beforehand is not known
Omitting the condition in for loop will lead to an if condition is not provided in while loop, it will
infinite loop cause a compilation error.

Syntax: Syntax:

for(initialization-part ; condition; inc/dcr-part) while(condition)

{ {

} }

While Do- while


● entry-controlled loop ● exit-controlled loop

(or) ( or )

enter restricted loop exit restricted loop

● helpful in situations where numbers of ● helpful in situations where numbers of


iterations is not known. iterations is not known

The statements gets executed only if the The statements gets executed once even if
condition is true . the condition is false.

● Cannot be used when we need to ● Can be used when we need to display


display a menu to the user a menu to the user

Syntax: Syntax:

while(condition) do

{ {

} }

while(condition)
Variations of for loops:

https://www.youtube.com/watch?v=SxX1rboCwS0

· The Java for loop can have a number of variations.


1) More than one variable initialization can be done.
2) More than one increment / decrement part can be done.
3) Multiple test conditions can be combined using logical operators

JUMP statements

Jumping statements are control statements that transfer execution control from one point
to another point in the program. There are two Jump statements that are provided in the
Java programming language:
1. Break statement.
2. Continue statement.
3. Return (grade 10)
Break

The break statement can also be used to jump out of a loop.

This example stops the loop when i is equal to 4:

OUTPUT:

Continue

The continue statement breaks one iteration (in the loop), if a specified condition occurs, and
continues with the next iteration in the loop.This example skips the value of 4:
Terminologies

1) Finite loop
When the loop block is executed for a fixed number of times , the iterative
construct is called as finite loop . All the parameters (initial value , test condition
and step value ) are well defined.

2 types
a) Continuous loop
b) Step loop

Continuous loop Step loop

The control variable is either incremented The control variable is either incremented
or decremented by 1 after each iteration . or decremented by a given value other
than 1 after each iteration .

for(i=1;i<=5;i++) for(i=1;i<=5;i+=2)
{ {

} }

Explanation : Explanation :

2) Infinite loop

● An infinite loop in Java is a sequence of instructions that loops indefinitely and never
come to an end unless the system crashes.
● Infinite loops in Java occur when the terminating condition of the loop is not met.
● This type of loop can be created by omitting one or more parameters.
Eg: 1

for(i=1;i<=3;)
{
Body of loop
}

Here , the step value is missing and hence its value will always be 1.So the test condition
will always be true.

Eg 2:

for(;;;)
{
Body of loop
}

Here all the parameters are missing .so it becomes an infinite loop.

While statement / do while statement

● A while statement using true as its parameter will create an infinite loop.

Eg 1:
do
{ while(true)
a= b+c; {
} a= b+c;
while(true); }

Eg 2:

int a =5;
while(a>0)
(
a++;
}

This type of looping constructs can be terminated with the help of break statement.

do
{
a= b+c;
if(c>=5)
break;
}
while(true);

Finite loop Infinite loop

● An infinite loop in Java is a sequence


● When the loop block is executed for of instructions that loops indefinitely
a fixed number of times , the and never come to an end unless the
iterative construct is called as finite system crashes.
loop .

● All the parameters (initial value , ● Infinite loops in Java occur when the
test condition and step value ) are terminating condition of the loop is
well defined. not met.

● This type of loop can be created by not ● This type of loop can be created by
omitting any parameters omitting one or more parameters

Example: Example:

Delay or null loop


A loop that does not contain any statement to be repeated is called “Delay loop” or “ Null loop”.
Null loops are used to create a delay in execution . It is used to pause the control as needed by
the user during programming .

Example :

s= 4+5;
for (i=1; i<=100;i++)
{

Here the loop executes 100 times without carrying any useful operation .

Inter-conversion of loops
We can convert the repetitive logic written using one type of loop into any of the other 2 types.
For example, if some repetitive logic is coded using a for loop, we can convert that for loop into
while or do-while loop. This is termed inter-conversion of loops.

Different ways to inter-convert the loops

1. for loop to while loop


2. for loop to do-while loop
3. do-while loop to while loop
4. do-while loop to for loop
5. while loop to do-while loop
6. while loop to for loop

Examples:
1) Rewrite the below program using do while
https://www.knowledgeboat.com/learn/class-9-icse-understanding-computer-applications-b
luej/solutions/Y3aqv/iterative-constructs-in-java

You might also like