*Looping Statement*
Page | 22
1. What is Looping Statement?
In real life we come across situations when we need to perform a set of task repeatedly till
some condition is met.
Such as – sending email to all employees, deleting all files and printing 1000 pages of a
document. All of these tasks is handled by looping statements.
Looping is the Programming concept to perform the given jobs/task various times then this
concept is called looping Statement.
2. Operators of Looping Statement
Operator Name
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
++ increment
-- decrement
3. Types of Looping Statement
C supports three looping statements.
1. for() loop
2. while() loop
3. do...while() loop
4. Syntax of for Loop
for(variable-initialization ; condition ; variable-update)
{
// Body of for loop
}
Parts of for loop
[Link]-initialization:-
It define starting point of the repetition (where to start loop). Page | 23
[Link]:-
it contain Boolean expressions and works like if...else.
if Boolean expression is true, and then execute body of loop otherwise terminate
the loop.
[Link]-update:-
it contains loop counter update (increment/decrement) statements.
5. Flowchart of For Looping Statement
6. Example of for, while & do-while Loop
1. Write a program in Java to print Your Name 10 times.
For Loop: Page | 24
class Loop
{
public static void main(String args[])
{
for(int i=1;i<=10;i++)
{
[Link]("Dileep");
}
}
}
While Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
[Link]("Dileep");
i++;
}
}
}
do - while Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
do
{
[Link]("Dileep");
i++;
}
while(i<=10);
}
}
2. Write a program in Java to print natural numbers from 1 to 10.
class Loop
{
public static void main(String args[])
{ Page | 25
int i;
for(i=1;i<=10;i++)
{
[Link](i);
}
}
}
3. Write a program in Java to print natural numbers from 10 to 1.
class Loop
{
public static void main(String args[])
{
int i;
for(i=10;i>=1;i--)
{
[Link](i);
}
}
}
4. WAP in Java to print multiplication table.
import [Link].*;
class Table
{
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any number for table:");
int n= [Link]([Link]());
for(int i=1;i<=10;i++)
{
int t=n*i;
[Link](n +"*"+ i +"=" +t);
}
}
}
5. WAP in java to find factorial of a number.
import [Link].*;
class Factorial
{
public static void main(String args[]) throws IOException Page | 26
{
int n,i, f=1;
BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter any number for fatorial:");
n= [Link]([Link]());
for(i=1;i<=n;i++)
{
f=f*i;
}
[Link]("factorial is="+f);
}
}