0% found this document useful (0 votes)
26 views9 pages

Iteration Constructs IN JAVA - 23 - 10 - 23

The document discusses different types of iteration constructs in Java including for, while, and do-while loops. It explains the parts of a loop and provides examples of using each loop type. It also covers topics like nested loops, empty loops, infinite loops, and differences between break and continue statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Iteration Constructs IN JAVA - 23 - 10 - 23

The document discusses different types of iteration constructs in Java including for, while, and do-while loops. It explains the parts of a loop and provides examples of using each loop type. It also covers topics like nested loops, empty loops, infinite loops, and differences between break and continue statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Iteration Constructs IN JAVA- PART 1

Iteration statements
The iteration statements allow a set of instructions to be performed repeatedly
until a certain condition is fulfilled. Java provide three kinds of loops:
 for loop (entry controlled loop)
 while loop (entry controlled loop)
 do-while loop (exit controlled loop) Parts of a loop

1. Initialization expression- The initialization of the loop variable takes


place under initialization expression. The initialization is executed only once, in
the beginning of the loop.

2. Test expression – The test expression is an expression whose truth value


decides whether the loop body will be executed or not. If the test expression
evaluates to true, the loop body gets executed otherwise the loop is
terminated.

3. Update expression – The update expression changes the value of the


loop variable.

4. The body of the loop - The statements that are executed repeatedly
form the body of the loop.

The for loop


The general form of for loop is :
The for loop variations
a. Multiple initialization and update expressions
A for loop may contain multiple initialization and update expressions. The
multiple expressions must be separated by commas.
Example:
for(i=1, sum=0;i<=3; sum+=i,++i)
System.out.println(sum);
b. Optional expressions
In a for loop, the initialization expression, test expression and update
expression are optional.
Skipping initialization expression
for( ; test expression; update expression)
loop body
Example:
Int i=1, sum=0;
for( ;i<=2;i++)
{
System.out.println(i);
}
Skipping update expression
for(initialization expression; test expression;)
loop body Example:
for(int i=0 ; i<=2 ; )
{
System.out.println(i);
i++;
}
a. Infinite loop
An infinite for loop is created by omitting the test expression.
for(j=25; ; i--)
System.out.println(j);

for( ; ; )
System.out.println(“Endless for loop”);
b. Empty loop or Null loop or Delay loop
If a loop doesnot contain any statement in its loop- body, it is said
to be an empty loop.
Example: for(i=1;i<=100;i++);
Int i,s=1;

for(i=2;i<=6;s +=i,i++); # this is an empty loop I more time

System.out.println(s);
c. Declaration of variables inside loops and if
A variable declared within an if or for/while/do-while statement
cannot be accessed after the statement is over.

THE while LOOP


The syntax of while loop is

Initialization;
while(test expression)
{
Loop body
Update expression
}

A while loop can be an empty loop or infinite loop.

THE do while LOOP

The syntax of do while loop is


Initialization;
do {
Loop body
Updation;
} while(test expression);

Nested loop
A loop that contains another loop in its body is called nested loop. The
inner loop must terminate before the outer loop.

for (int i = 1; i <= 3; i++) {


for (int j = 1; j <= 2; j++) {
System.out.print(i * j + " ");
}
}

Comparison of Loops
The for loop is appropriate when you know in advance how many times
the loop will be executed.
The while loop is preferred when you don’t know in advance how many
times the loop will be executed.
do while loop should be preferred when you are sure to execute the loop
body at least once.
Jump statements
Java has three statements that perform an unconditional branch
•return
•break
•continue
System.exit() is used to come out of a program.
The return statement is used to return from a function.
Difference between break and continue

Assignments:
Identify all errors in the following iterative statements.
(i) for (int i=5; i>0; i++)
{
System.out.println(“Java”);
}
Give the output of the code given below:
1. for (int i = 1; i <= 5; i++)
{
System.out.print(i + " ");
}

2. for (int i = 5; i > 0; i--) {


System.out.print(i + " ");
}
Solution 2: A) 5 4 3 2 1

3. for (int i = 1; i <= 5; i++) {


if (i % 2 == 0) {
continue;
}
System.out.print(i + " ");
}
Solution 7: A) 1 3 5

4. for (int i = 1; i <= 3; i++) {


for (int j = 3; j >= 1; j--) {
System.out.print(i * j + " ");
}
}
Solution 10: A) 3 2 1 6 4 2 9 6 3

5. int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
sum += i;
}
}
System.out.println(sum);
Solution 12: 6

1. Write a program to find the sum of first 10 natural numbers.


2. Write a program to print the square of the numbers from one to ten in the
format given below.
Output
The square of 1=1
The square of 2=4
The square of 3=9
The square of 4=16
The square of 5=25
The square of 6=36
The square of 7=49
The square of 8=64
The square of 9=81
The square of 10=100
3. Write a program to print the first 10 odd numbers
4. Write a program to print the multiplication table of a number
5. Write a program to input a number and find its factors
6. Write a program to find the factorial of a number.
7. Write a program to print the first 20 numbers in the reverse order.
factors class SL5 {
public static void main()
{ int i,n=7;
System.out.println("The factors are");
for(i=1;i<=n/2;i++)
{
if (n%i==0)
System.out.println(i);
}
}
}

You might also like