0% found this document useful (0 votes)
39 views16 pages

IterativeStatements

The document discusses different types of iterative/looping statements in Java including while loops, do-while loops, for loops, and for-each loops. It provides syntax examples and sample code to illustrate how to use each type of loop.

Uploaded by

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

IterativeStatements

The document discusses different types of iterative/looping statements in Java including while loops, do-while loops, for loops, and for-each loops. It provides syntax examples and sample code to illustrate how to use each type of loop.

Uploaded by

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

Iterative/Looping Statements

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;
}

The control flow of the while loop


false while(expression)
{ true Check condition

True block
}
Next statement

Control flow of while loop

1. Declare and Initialize a variable with starting value


2. Use that variable in condition
3. Increment or decrement(update the variable)

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

import static java.lang.System.*;


import java.util.*;
class DoWhileDemo
{ public static void main(String args[])
{
Scanner s=new Scanner(in);
String opt="";
do{
out.println("1. add");
out.println("2. Sub");
out.println("3. Mul");
out.println("4. Div");
out.println("Enter option");
int n=s.nextInt();
out.println("Enter two int values..");
int a=s.nextInt();
int b=s.nextInt();
int c=0;
switch(n)
{
case 1:
c=a+b;
break;
case 2:
c=a-b;
break;
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
default: out.println("Wrong option");
}
out.println("C:\t"+c);
out.println("Do you wonnaa continue...(yes/no)");
opt=s.next();
}while(opt.equals("yes"));
}
}

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

import static java.lang.System.*;


import static java.lang.Integer.*;
import java.io.*;
class ForDemo
{ public static void main(String args[])throws Exception
{ InputStreamReader isr=new InputStreamReader(in);
BufferedReader br=new BufferedReader(isr);
out.println("Enter n value...");
int n=parseInt(br.readLine());
for(int i=1,j=10;i<=n;i++,j--)
{out.println(i+"\t"+j);
}
}
}

Example: ForDemo2.java

import static java.lang.System.*;


import java.util.*;
class ForDemo2
{ public static void main(String args[])
{ int arr[]={10,20,30,40,50,60,70,80};
out.println("Elements in an array");
for(int i=0;i<arr.length;i++)
{out.println(arr[i]+"\t");
}
}
}
Assignment.java

import static java.lang.System.*;


import java.util.*;
class Assignment
{ public static void main(String args[])
{ Scanner s=new Scanner(in);
out.println("How many int elements you want to enter");
int n=s.nextInt();
String evenNos="";
String oddNos="";
int inputValue;
for(int i=1;i<=n;i++)
{ out.println("Enter element:\t"+i);
inputValue=s.nextInt();
if(inputValue%2==0)
{
evenNos=evenNos+inputValue+"\t";
//out.println(inputValue+" is even");
}
else
{
//out.println(inputValue+" is odd");
oddNos=oddNos+inputValue+"\t";
}
}
out.println("Even Nos:\t"+evenNos);
out.println("Odd Nos:\t"+oddNos);
}
}

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...)

The enhanced for loop


Syntax:

for (variable: collection)


{ Statements;
}

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.

Example: for(int ele:a)


Out.println(ele);
// You should read this loop as “for each element in a”.

ForEachDemo.java

import static java.lang.System.*;


import java.util.*;
class ForEachDemo
{ public static void main(String args[])
{ int arr[]={10,20,30,40,50};
String names[]={"priya","madhu","vinnu","prannu"};
out.println("values....");
for(int v:arr)
{out.println(v);
}
out.println("Names....");
for(String name:names)
{out.println(name);
}
//Vector is a collection
Vector<String> v=new Vector<String>();
v.add("giri");
v.add("sobha");
v.add("shekar");
v.add("madhu");
v.add("narayanarao");
v.add("krishnaveni");
out.println("Names....");
for(String name:v)
{out.println(name);
}
}
}

Program to print 1 to 10 numbers

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

Program to print 1 to 10 even numbers

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

Example: program to print 1 to n odd numbers

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

Write a program to print factors of a given number

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

Print 1 to n even numbers

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

Program to print 1 to n odd numbers

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

Write a program to print factors count of a given number

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

Program to print whether the given number is prime or not

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

Write a program to find sum of 1 to n natural numbers

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

Write a program to print prime numbers between 1 to n

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[])
{

Scanner s=new Scanner(System.in);


System.out.println("Bhagavan ke naam sey kuch paisey dedo bhayya:\t");
int n=s.nextInt();
int i=1;
System.out.printf("Prime Numbers between %d to %d%n",i,n);
while(i<=n)
{
//System.out.println(isNotPrime(i)?i+" is Prime":i+" Is Not Prime");
if(isPrime(i))
System.out.printf("%03d%n",i);

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

Program to print reverse of a given number

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);
}
}

You might also like