0% found this document useful (0 votes)
14 views5 pages

Looping

This document discusses looping statements in programming. It defines looping as repeatedly performing a set of tasks until a condition is met. There are three main types of looping statements: for loops, while loops, and do-while loops. For loops have three main parts - an initialization statement, a condition, and an update statement. While and do-while loops repeat as long as a condition is true. Examples are given to illustrate each type of loop, such as printing a name 10 times or calculating a factorial.

Uploaded by

ganiyagamer12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Looping

This document discusses looping statements in programming. It defines looping as repeatedly performing a set of tasks until a condition is met. There are three main types of looping statements: for loops, while loops, and do-while loops. For loops have three main parts - an initialization statement, a condition, and an update statement. While and do-while loops repeat as long as a condition is true. Examples are given to illustrate each type of loop, such as printing a name 10 times or calculating a factorial.

Uploaded by

ganiyagamer12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

*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
i.Variable-initialization:-
It define starting point of the repetition (where to start loop). Page | 23
ii.Condition:-
it contain Boolean expressions and works like if...else.
if Boolean expression is true, and then execute body of loop otherwise terminate
the loop.
iii.Variable-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++)
{
System.out.println("Dileep");
}
}
}

While Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println("Dileep");
i++;
}
}
}

do - while Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
do
{

System.out.println("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++)
{
System.out.println(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--)
{
System.out.println(i);
}
}
}
4. WAP in Java to print multiplication table.
import java.io.*;
class Table
{
public static void main(String args[]) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter any number for table:");

int n= Integer.parseInt(in.readLine());
for(int i=1;i<=10;i++)
{
int t=n*i;
System.out.println(n +"*"+ i +"=" +t);
}
}
}
5. WAP in java to find factorial of a number.
import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException Page | 26
{
int n,i, f=1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter any number for fatorial:");

n= Integer.parseInt(in.readLine());
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("factorial is="+f);

}
}

You might also like