0% found this document useful (0 votes)
58 views

Loops in JAVA

The document discusses different types of loops in Java including for, while, and do-while loops. It provides examples of using for and while loops to iterate through ranges of numbers and print outputs. For loops are used when the number of iterations is known, while while loops are used when the number of iterations is unknown. The examples demonstrate using loops to print natural numbers, even/odd numbers, numbers in reverse order, and other patterns up to a given limit.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Loops in JAVA

The document discusses different types of loops in Java including for, while, and do-while loops. It provides examples of using for and while loops to iterate through ranges of numbers and print outputs. For loops are used when the number of iterations is known, while while loops are used when the number of iterations is unknown. The examples demonstrate using loops to print natural numbers, even/odd numbers, numbers in reverse order, and other patterns up to a given limit.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ITERATIVE CONSTRUCS IN JAVA

Iteration means repeated execution of a set of statements. This can be achieved by using a loop.
There are three types of loops are available in Java.
 for Loop
 while loop
 do-while loop

for loop
for loop is used when number of iterations is fixed and known. It enables a particular set of
conditions to be executed repeatedly until a condition is satisfied.

Components of ‘for’ loop structure

for (a=1; a<=10; a++)

  
control variable test condition Increment/
decrement
value

Write a Java program which will print first ten natural number (using for loop)

class EIGHT
{
public static void main()
{
int i;
for(i=1;i<=10;i++)
System.out.println(i);
}
}

Write a Java program which will print even numbers from 1 to 30 (using for loop)

class EIGHT
{
public static void main()
{
int i;
for(i=2;i<=30;i=i+2)
System.out.println(i);
}
}
Write a Java program which will print numbers from 1 to up to nth term. Where n is a limit
given by the user. (using for loop)

class EIGHT
{
public static void main(int n)
{
int i;
for(i=1;i<=n;i++)
System.out.println(i);
}
}

Write a Java program which will print numbers in reverse order from 30 to 1 (using for
loop)

class EIGHT
{
public static void main()
{
int i;
for(i=30;i>=1;i--)
System.out.println(i);
}
}

Write a Java program which will print Calcutta Boys’ School up to n times. Where
n will be given by the user. (using for loop)

class EIGHT
{
public static void main(int n)
{
int i;
for(i=1;i<=n;i++)
System.out.println("Calcutta Boys' School");
}
}

Write a Java program which will print odd numbers from 1 to 30 (using for loop)

class EIGHT {
public static void main() {
int i;
for(i=1;i<=30;i=i+2) {
System.out.println(i);
}} }
Write a Java program which will print following series up to nth terms. Where n will be
given by the user. (using for loop)

12, 24, 36, 48, 60 ……..up to nth term

class EIGHT
{
public static void main(int n)
{
int i=1;
int number=12;
for(i=1;i<=n;i++)
{
System.out.println(number*i);
}
}
}

Write a Java program which will print sum and product of numbers from 1 to 15
(using for loop)

class EIGHT
{
public static void main()
{
int i, sum=0, pro=1;
for(i=1;i<=15;i++)
{
sum=sum+i;
pro=pro*i;
}
System.out.println("Sum of 1 to 15 ="+sum);
System.out.println("Product of 1 to 15 ="+pro);
}
}

while loop
Sometimes, it may happen that a set of statements are to be executed repeatedly without
knowing the number of iterations while loop is used to solve the purpose.

while (a<10)


test condition/expression
Write a Java program which will print first ten natural number (using while loop)

class EIGHT
{
public static void main()
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}

Write a Java program which will print numbers from 1 to up to nth term. Where n is a limit.
(using while loop)

class EIGHT
{
public static void main(int n)
{
int i=1;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}

Write a Java program which will print numbers in reverse order from 30 to 1 (using while
loop)

class EIGHT
{
public static void main()
{
int i=30;
while(i>=1)
{
System.out.println(i);
i--;
}
}
}
Write a Java program which will print Calcutta Boys’ School up to n times. Where
n will be given by the user. (using while loop)

class EIGHT
{
public static void main(int n)
{
int i=1;
while(i<=n)
{
System.out.println("Calcutta Boys' School");
i++;
}
}
}

Write a Java program which will print following series up to nth terms. Where
n will be given by the user. (using while loop)

12, 24, 36, 48, 60 ……..up to nth term

class EIGHT
{
public static void main(int n)
{
int i=1;
int number=12;
while(i<=n)
{
System.out.println(number*i);
i++;
}
}
}

Write a Java program which will print odd numbers from 1 to 30 (using while loop)

class EIGHT {
public static void main() {
int i=1;
while(i<=30)
{
System.out.println(i);
i=i+2;
}
}
}

You might also like