IterativeStatements
IterativeStatements
If you want to execute 1 or more statements repeatedly no.of times until given condition false.
1. While loop
2. Do…while
3. For
4. Foreach
While loop
The while loop is used when the number of iterations to be performed are not known in advanced, While
loop executes a statement or statements (which may be block of statements) while the condition is true.
Once the condition is false, then the loop will be terminated.
Syntax:
while(condition){
statements;
}
True block
}
Next statement
WhileDemo.java
import java.util.*;
class WhileDemo1
{
public static void main(String args[])
{
//1,2,3
int i=1;
Scanner s=new Scanner(System.in);
System.out.println("Bhagavan ke naam sey kuch paisey dedo bhayya:\t");
int n=s.nextInt();
//i=1
//n=3
while(i<=n)
{
System.out.println("Welcome");
i++; //i=2,3,4
}
}
}
Output:
Bhagavan ke naam sey kuch paisey dedo bhayya:
5
Welcome
Welcome
Welcome
Welcome
Welcome
WhileDemo.java
import java.util.*;
class WhileDemo1
{
public static void main(String args[])
{
//1,2,3
int i=1;
Scanner s=new Scanner(System.in);
System.out.println("Bhagavan ke naam sey kuch paisey dedo bhayya:\t");
int n=s.nextInt();
//i=1
//n=3
while(i<=n)
{
System.out.println(i);
i++; //i=2,3,4
}
}
}
Output:
Bhagavan ke naam sey kuch paisey dedo bhayya:
5
1
2
3
4
5
do..while loop
If we want to execute the whole body of a while loop at least once, even though test expression returns
false, we have to use do..while loop.
Syntax:
do{ statements;
}
while(condition);
DoWhile.java
For loop:
A for loop is used to execute, set of statements, for a specific no.of times. Syntax is given below.
Syntax:
for(initialization;condition;increment/decrement){ statements;
}
Example: ForDemo1.java
Example: ForDemo2.java
For-each loop
It was introduced in Java SE 5.0. It is very power full looping construct that allows you to get the
elements of an array (or) collection directly without having to-do with index values and conditional
checking.
What is collection?
A collection is a group of elements like integer, float, char, or objects. Examples for collections are I)
Arrays and II) classes of java.util package (like Stack, ArrayList, LinkedList, Vector, etc...)
Here the given variable is attached to the collection. This variable represents each element of the
collection one by one. If, the collection contains 10 elements then this loop will be executed 10 times and
the variable will represent these elements one by one.
ForEachDemo.java
class WhileDemo1
{
public static void main(String args[])
{
//1,2,3,4,5,6,7,8,9,10
int n=10;
int i=1;
//i=1,2,3
//n=10
while(i<=n)
{
System.out.println(i); //1,2
i++;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java WhileDemo1
1
2
3
4
5
6
7
8
9
10
import java.util.*;
class EvenDemo
{
public static void main(String args[])
{
int i=2;
//i=2,4,6,8,10,12
while(i<=10)
{ System.out.println(i); //2,4,6,8,10
i=i+2;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>javac EvenDemo.java
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java EvenDemo
2
4
6
8
10
import java.util.*;
class OddNumbers
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter n value:\t");
int n=s.nextInt();
int i=1;
//i=1,3
while(i<=n)
{
System.out.println(i); //1,3
i=i+2;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java OddNumbers
Enter n value:
10
1
3
5
7
9
import java.util.*;
class FactorsDemo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=.nextInt();
//1,2,3,4,5
int i=1;
while(i<=n)
{
if(n%i==0)
System.out.println(i);
i++;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java FactorsDemo
Enter n value:
20
1
2
4
5
10
20
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
//1,2,3,4,5
int i=1;
while(i<=n)
{
if(i%2==0)
System.out.println(i);
i++;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Demo
Enter n value:
10
2
4
6
8
10
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
//1,2,3,4,5
int i=1;
while(i<=n)
{
if(i%2!=0)
System.out.println(i);
i++;
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Demo
Enter n value:
10
1
3
5
7
9
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
//1,2,3,4,5
int i=1;
int count=0;
while(i<=n)
{
if(n%i==0)
{count++;
}
i++;
}
System.out.println("No. of factors of "+n+" Is "+count);
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Demo
Enter n value:
100
No. of factors of 100 Is 9
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
//1,2,3,4,5
int i=1;
int count=0;
while(i<=n)
{
if(n%i==0)
{count++;
}
i++;
}
if(count==2)
System.out.println(n+" Is Prime");
else
System.out.println(n+" Is Not a Prime");
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Demo
Enter n value:
11
11 Is Prime
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int i=1;
int sum=0;
while(i<=n)
{
System.out.println(i);
sum=sum+i;
i++;
}
System.out.println("Sum="+sum);
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Demo
Enter n value:
10
1
2
3
4
5
6
7
8
9
10
Sum=55
SumOfNaturalNumbers
import java.util.*;
class SumOfNaturalNos
{
public static void main(String args[])
{
System.out.println("Enter n value:\t");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int sum=n*(n+1)/2;
System.out.println("Sum="+sum);
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java SumOfNaturalNos
Enter n value:
10
Sum=55
Write a program to check whether the given number is prime or not using user defined function
import java.util.*;
class Prime
{
static boolean isPrime(int n)
{ //n=5
int count=0;
int i=1;
while(i<=n)
{
if(n%i==0)
count++;
i++;
}
if(count==2)
return true;
else
return false;
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter n value:\t");
int n=s.nextInt();//5
boolean prime=isPrime(n);
if(prime)
System.out.println(n+" Is Prime");
else
System.out.println(n+" Is Not A Prime");
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Prime
Enter n value:
10
10 Is Not A Prime
C:\Users\hp\Documents\ByteXL\Java\mic_cse_2ndYears\Programs>java Prime
Enter n value:
5
5 Is Prime
import java.util.*;
class RangeOfPrimes
{
static boolean isPrime(int n)
{
//n=5
int i=1;
int count=0;
while(i<=n)
{
if(n%i==0)
{ count++;
//System.out.println(i);
}
i++; //i=2,3,4
}
if(count==2)
return true;
else
return false;
}
public static void main(String args[])
{
i++;
}
}
}
Output:
Bhagavan ke naam sey kuch paisey dedo bhayya:
20
Prime Numbers between 1 to 20
002
003
005
007
011
013
017
019
import java.util.Scanner;
class DigitsOfNo
{
public static void main(String args[])
{ Scanner s=new Scanner(System.in);
int n=s.nextInt();
int rev=0;
while(n>0)
{ int r1=n%10; //r1=3,5,1
n=n/10; //n=0
rev=rev*10+r1;
System.out.println(rev);
}
}
}
Output:
C:\Users\hp\Documents\ByteXL\Java\mic_ece\Programs>java DigitsOfNo
153
3
35
351
DoWhile Example
import java.util.Scanner;
class DoWhile
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter n value:\t");
int n=s.nextInt();
int i=1;
//n=5
//i=1
do{
System.out.println(i);
i++;//i=2,3,4,5,6
}while(i<=n);
}
}