Java Iterations tutorial
Learn Iteration(looping) Statements in Java
Iteration(looping) statements:
Iteration: means repeating. Repeats a block of statements for certain number of times.
Types of Loops in java.
1. while 2. do-while 3. for 4. for-in
1. while-statement.
syn:
while(condition)
{
..............;
..............;
}
Logic:
ex: int i=0;
while(i<=10)
{
System.out.println(i);
i++;
}
It checks i value with the condition
if i is less than 10 then it enters inside the while and executes print-statement and increments the i value.
Again it goes to condition and checks the incremented value(now i=2) with condition. Now
2 is <10..?
Yes, so again enters inside the while prints the i, and increments i value. This will continue up to i is ≤ 10.
When i becomes 11, the condition becomes false and comes out of the loop.
ex:
Print Natural Numbers using while loop.
public class Nat
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i++;
}
}
}
Program to print odd numbers:
ex: 1,3,5,7,9 etc.
Logic:
1) initialize i to 1.
2) print i.
3) increment i by 2.
Print odd-nos
public class Oddnumbers
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i=i+2;
}
}
}
Program to print even numbers:
ex:
print 0,2,4,8,10
Logic:
1) initialize i to 0.
2) print i.
3) increment i by 2.
Printing Even numbers:
class Evennumbers
{
public static void main(String args[])
{
int i=0;
while(i<=10)
{
System.out.println(i);
i=i+2;
}
}
}
Summary of while
1.printing 10 natural numbers 2.printing odd numbers. 3 printing even numbers.
Try out:
Printing 10 natural no. in Reverse Order.
1) i=10 //initial value
2) i>=1 //condition
3) i-- //decrementing
Factorial of given number.
n=5
1*2*3*4*5=120
n=5
f=1
n>=0
f=f*(n-1);
n--
print f.
Sum of the digits of a given number.
logic:
1+2+3=6
sum=0;
n=123
r=n%10
sum=sum+r;
print sum;
Printing of Mathematical Table using while
expression:
n=5
i=1
while(i<10)
{
System.out.println( n + "*" + i + "=" + (n*i) );
i++;
}
o/p:
-----
1*1=1
1*2=2
1*3=3
.
.
1*10=10
Nested while: while inside another while is called nested while
while(condition)
{
while(condition)
{
.......
.......
}
}
Write a program to the following output:
1 2 1st iteration 3 --- 1 2 2nd iteration 3 --- 1 2 3rd iteration 3
example:
import java.io.*;
public class Matrix
{
public static void main(String args[]{
int j;
int i=1;
while(i<=3)
{
j=1;
while(j<=3)
{
System.out.println(j);
j++;
}
System.out.println("-------");
i++;
}
}
}
Print 1-3 mathematical tables:
replace print j stmt as below..
System.out.println(i+"*"+j+"="+(i*j));
2. do..while-statement.
syn:
do{
...........;
...........;
}
while(condition);
ex:
class Do_whileDemo
{
public static void main(String args[]){
int i=1;
do{
System.out.println(i);
i++;
}
while(i<=10);
}
}
Difference between while--do-while in Java
- In while 1st condition checks, then executes the block
- do while 1st executes the block, and then checks the condition
- do-while at least once it is executed before checking the condition
3. for - Loop.
For Loop
Nested for
Break and Continue
For loop contains 3 parts in it. each part is separater with semi colon';'.
1. initialization
2. condition
3. incrementation
syn: for(initilization; condition; incrementation)
{
..............;
..............;
}
ex:
for(int i=0; i<=10; i++)
{
System.out.println(i);
}
break and continue in Java:
breaks ---> breaks the loop if the given condition is true.
continue--> continue skips the current iteration in a loop.
break code example:
class Breakingloop
{
public static void main(String args[])
{
int k;
int n = 20;
int sum = 0;
for(k=n; k>=1; k--)
{
if(k==10)
break;
System.out.println(k);
sum = sum + k;
}
System.out.println("sum=" + sum);
}
}
continue code example:
public class Continueinloop
{
public static void main(String args[])
{
int k;
int n = 10
int sum=0;
for(k=n;k>=1;k--)
{
if(k==5)
continue;
System.out.println(k);
sum=sum+k;
}
System.out.println("sum=" + sum);
}}
Nested for:
One forloop inside, another forloop
syn:
for(initilization;condition;incrementation)
{
for(initilization;condition;incrementation)
{
..............;
..............;
}
}
EX1:
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
System.out.println(j);
}
}
ex2:
public class Nestedfor
{
public static void main(String args[])
{
int i,j,k;
k=1;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
System.out.print(k++);
}
System.out.println();
}
}
}
---------------------------------------------------------
Print the following matrix(3/3) using nested for.
100
010
001
Logic:
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(i!=j)
System.out.print(0);
else
System.out.print(1);
}
System.out.println();
}
------------------------------------------------------------
import java.lang.io;
class Loopfor
{
public static void main(String args[])
{
int k;
int n=Integer.parseInt(args[0]);
int sum=0;
for(k=n;k>=1;k--)
{
if((k%2)!=0)
{
System.out.println(k);
sum=sum+k;
}
}
System.out.println("sum="+sum);
}
}
Different ways of for..loop
syn2:
i=0;
for(;i<10;i++)
{
...........;
...........;
}
---------------
syn3:
i=0
for(;i<10;)
{
....;
....;
i++;
}
---------------
syn4:
for(; ;) this goes to infinite loop.
4. for..in
syn:
for(itr-var : collection)
ex:
for(int i:n)
{
.....;
.....;
}
---> n is array collection
---> i is iterating variable
for each:
public class Foreach{
public static void main(String args[]) { int names[] = {"anna","jenny","sofie","lisa","rossy"}; for(int i : names) { System.out.println(i); } } }
Comments
Post a Comment