Theory
Theory
Theory
a)while loop
b)for loop
a) do-while 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.
2 types
So we must know in advance how many The loop terminates defending upon conditions
repetitions may be needed. involving boolean flags.
Syntax:
while(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:-
Syntax:-
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.
· 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
Example explained
· 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:
{ {
} }
(or) ( or )
The statements gets executed only if the The statements gets executed once even if
condition is true . the condition is false.
Syntax: Syntax:
while(condition) do
{ {
} }
while(condition)
Variations of for loops:
https://www.youtube.com/watch?v=SxX1rboCwS0
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
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
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.
● 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);
● 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:
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.
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