Loops
There will be a situation where we want to repeat particular statements again and again. For this we have to write those statements again, instead we use the loops to do the same.
To print your name 10 times without using a loop goes like this:
class example
{
public static void main( )
System.out.println(“ BCBS”);
System.out.println(“ BCBS”);
System.out.println(“ BCBS”); 10 times it should be repeated.
System.out.println(“ BCBS”);
System.out.println(“ BCBS”);
System.out.println(“ BCBS”); 10 times it should be repeated.
System.out.println(“ BCBS”);
………………………
……………………….
…………………………
……………………….
…………………………
}
}
Using loops we simplify the program.
Types of Loops :
1. for loop
2. while loop
3. do while loop
for loop
for(intial value ; condition ; update)
note there is no semicolon after for statement.
The above program can be written like this.
class example
{
public static void main( )
{
int i;
System.out.println(“ BCBS”);
}
}
The initial value is 1, BCBS will be printed since the condition is true, i.e i is less than 10. Then it gets incremented to 2, again condition is true so BCBS will be printed again. It goes on till I is equal to 10. Once it is updated the value becomes 11, which does not satisfy the condition. so the loop ends here, control goes to the next line.
}
}
The initial value is 1, BCBS will be printed since the condition is true, i.e i is less than 10. Then it gets incremented to 2, again condition is true so BCBS will be printed again. It goes on till I is equal to 10. Once it is updated the value becomes 11, which does not satisfy the condition. so the loop ends here, control goes to the next line.
while loop
This the entry controlled loop, because the loop will be executed only if the condition is true.
initial value;
while(condition)
{
update;
}
Note there is no semicolon after while statement.
class example
{
public static void main( )
{
{
System.out.println(“ BCBS”);
i++; i is incremented by 1.
}
}
}
}
}
after i++ , the control will go back to while statement to check the condition.
do while loop
This loop is called exit controlled loop. Because the statements will be executed once ,even if the condition is false .In the end of the loop, it checks the condition.
initial value;
do
{
update;
update;
1.
class sum
{
public static void main( int x,int n) x and n values should be given
{
public static void main( int x,int n) x and n values should be given
int i, s=0; initial value should be given.
for(i=1;i<=n;i++)
s=s+(x/i);
System.out.print(“Sum=”+s);
}
}
Note : if the question is + then
}
}
Note : if the question is
s=s+(Math.pow(x,i)/i);
2. Separate the digits in a number.
(e.g) 139,
1, 3,9 should be printed separately.
while(n>0)
{
System.out.println(d);
n=n / 10; Gives the quotient ,that is 13
}
since n is above zero it keeps repeating the statements, finally when n is less than zero ,it skips the loop.
Using the above logic the sum of the digits, reverse the number programs can be done.
3. Fibonacci series :
0, 1, 1, 2, 3, 5, 8, 13 …………………….
Add the previous two numbers to get the third number.
a=0;
b=1;
System.out.println(a );
System.out.println(a );
from 3,
while(ctr<=n)
{
c=a+b;
c=a+b;
System.out.println(c);
b=c; swap the values.
ctr++;
}
a
|
b
|
c=a+b
|
1
| ||
2
| ||
1
|
2
|
3
|
No comments:
Post a Comment