Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Loop Theory

There are three main types of loops in Java: for, while, and do-while loops. Loops are used to repeatedly execute a block of code while a condition is true. For loops are used when the number of iterations is known. While loops are used when the number is unknown. Do-while loops always execute the block at least once even if the condition is false.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Loop Theory

There are three main types of loops in Java: for, while, and do-while loops. Loops are used to repeatedly execute a block of code while a condition is true. For loops are used when the number of iterations is known. While loops are used when the number is unknown. Do-while loops always execute the block at least once even if the condition is false.

Uploaded by

zemo kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

LOOPING

Loop/iteration : The purpose of loop or iterative statements is to repeat a block


of Java statements several times depending upon the condition mentioned within the
loop. Three types of loop are present in java-
1- for loop
2- while loop
3- do while loop

There are two kinds of looping statements in Java.


1. Entry Controlled Loop
2. Exit Controlled Loop
Entry Controlled loop which is also known as Pre Tested Loop the condition of the
loop is first checked and then the loop body is executed, which means if the condition is
false the loop doesn’t get executed even for once.
E.g. a. for LOOP
b. while LOOP

Exit Controlled loop which is also known as Post Tested Loop the condition is
checked at the end of the loop body, which means even if the condition is false the loop
gets executed at least for once.
E.g. do…while LOOP

Many loops consist of three operations surrounding the body:


(1) initialization of a variable,
(2) testing a condition, and
(3) updating a value before the next iteration.
for loop : for loop is simple loop .we use for loop when number of times statements
is to be repeated is known.
Syntax:
for (initialization ; condition/test expression; update)
{
body
}
1. The initialization statement is done before the loop is started, usually to initialize an
iteration variable.
2. The condition/test expression is tested before each time the loop is done. The loop
isn’t executed if the boolean expression is false.
3. The update statement is done after the body is executed. It increments /decrement
loop control variable.

//… For loop to to display squares of natural nos. from 1 to 10


for (int i = 1; i <= 10; i++)
{
System.out.println(i*i);
}
while loop : The while statement is used to repeat a block of statements while some
condition is true.
Syntax:
initialization;
while (condition)
{
Body;
update statement;
}

//… While loop to display squares of natural nos. from 1 to 10


int i = 1;
while (i <=10)
{
System.out.println(i*i);
i++;
}

do…while loop : This loop is similar to the while loop except the test expression
occurs at the bottom of the loop. this ensures that the body executes at least one time.
Syntax
do
{
...
} while (condition);
Always remember that a do…while statement has a semi-colon at its end.

//…do while loop to display squares of natural nos. from 1 to 10


int i = 1;
do
{
System.out.println(i*i);
i++;
} while (i <=10);

If the body of a loop has more than one statement, you must put the statements inside
braces { }. If there is only one statement, it is not necessary to use braces { }.

Distinguish between:
(a) for and while loop

FOR LOOP WHILE LOOP


It is used when we know in advance It is used when we do not know the
how many repetitions are to be done number of repetitions are to be done.
The initialization, condition check and Here initialization, condition check and
update is done at one line. update is done at difference places.

(b) while and do-while loop

WHILE LOOP DO-WHILE LOOP


The while loop is entry-controlled, it do-while loop is exit-controlled, it check
check the condition first and then the the condition after executing the loop
execution begins once
The while loop will not execute at all if The do while loop will execute at least
the condition is not satisfied once.

State one difference and one similarity between while and do-while loop.
Similarity: Both while and do-while loops are used when the number of iterations is not
fixed.
Dissimilarity: The while loop is entry-controlled, but the do-while loop is exit-controlled.

State one similarity and one difference between while and for loop.
Similarity: Both while and for loops are entry-controlled loops.
Dissimilarity: The for loop is a suitable choice when the number of iterations is fixed. The while
loop is a suitable choice when the number of iterations is not fixed.

break statement: when a break statement is used to terminates the current loop and
program control resumes at the next statement following the loop.

for(int i=1;i<=5;i++)
{
if(i==3)
break;
System.out.print(i+” “);
}
System.out.print(“loop is over“);

Output is : 1 2 loop is over

continue statement : when a continue statement is encountered inside the body of


the loop, remaining statement are skipped and loop proceed with the next iteration.

for(int i=1;i<=5;i++)
{
if(i==3)
continue;
System.out.print(i+” “);
}
System.out.print(“loop is over“);

Output is : 1 2 4 5 loop is over

Different Looping Variation :

(a) Multiple initialization and updation of variables in a for loop.

We can initialize and update multiple variables in a for loop. These variables are separated
by comma (,).
for(i=1, j=10 ; i<=10 ; j-- , i++)
{
System.out.println("i=" +i+" "+"j="+j);
}

(b) Infinite loop : Infinite loop is a loop which executes indefinitely i.e. the given test-
expression is forever evaluated to true. For example,

for(int i=1;i>0;i++)
{
System.out.println(i);
}
The loop executes infinitely because the value of i is always greater than 0.

(c) Empty loop : Loop that is used to create a delay in the execution by creating a null
loop. If the loop doesn’t contain a body, it is called an empty loop. For example,

for(int i=1;i<=100;i++);

Notice the semicolon at the end of the parenthesis, indicating that the for loop
does not have body and therefore no statement is repeatedly executed again and
again.

Nested Loop : A loop within a loop is called a nested loop. The following example
shows how nested for loop works:
for(i=1;i<=3;i++)
{
for(j=1;j<=4;j++)
{
System.out.print(j);
}
System.out.println( );
}

The output received for the above program fragment is:


1234
1234
1234

You might also like