0% found this document useful (0 votes)
19 views

Class 10 JavaLoopingStatements

Java looping statements

Uploaded by

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

Class 10 JavaLoopingStatements

Java looping statements

Uploaded by

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

Java Looping statements:

Looping statements are used to iterate/repeat a piece of code continously until


given condition becomes false which reduces length of code and improves
readability. There are four looping statements available in Java langauge.

1. for: If we know the iterations in advance then we can use for loop.
2. while: If we know/don't know iterations in advance we can use while loop.
3. do-while: If we know/don't know iterations in advance and also task has to be
executed for once even if the condition is false in the begining then we can use
do-while.
4. for-each: It is used to traverse through array/collection. It is introduced in
JDK5.

1. for loop syntax:

for(initialization; condition; increment/decrement)


{
//for-loop-body
}

Step-1: initialization: This part includes assignment part of iteration variable.


It will be executed only once in the begining and control goes to condition part.

Step-2: condition: This part include an expression. If it is true then block of


statements will be executed. If condition is false then loop stops its iteration
process.

Step-3: for-loop-body: After checking condition, control comes inside of the loop
and executes body and goes to increment/decrement part.

Step-4: increment/decrement: After loop-body got executed, control comes to this


part increment/decrement value of iteration variable by 1. After this part, control
goes to condition part.

Step-5: From step-2 to step-4 will be repeated like this until condition becomes
false.

Task-1: read and print numbers from 1 to n.

Sample Input/Output-1:

Enter number: 5
Numbers from 1 to 5: 1 2 3 4 5

Sample Input/Output-2:

Enter number: 10
Numbers from 1 to 10: 1 2 3 4 5 6 7 8 9 10

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
range = s.nextInt();
System.out.print("Numbers from 1 to "+range+": ");
for(int i=1; i<=range; i++){
System.out.print(i+" ");
}
}
}

Task-2: read and print square, cube for 1 to n numbers.

Sample Input/Output-1:

Enter number: 3

Square, Cube for 1 -> 1, 1


Square, Cube for 2 -> 4, 8
Square, Cube for 3 -> 9, 27

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter range: ");
range = s.nextInt();

for(int i=1; i<=range; i++){


System.out.println("Square, Cube for "+i+" -> "+i*i+", "+i*i*i);
}
}
}

Task-3: read and print square, cube for n different inputs.

Sample Input/Output-1:

Enter number of inputs: 3

Enter number-1: 3
Square, Cube for 3 -> 9, 27

Enter number-2: 2
Square, Cube for 2 -> 4, 8

Enter number-3: 5
Square, Cube for 5 -> 25, 125

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();

for(int i=1; i<=range; i++){


int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
System.out.println("Square, Cube for "+n+" -> "+n*n+", "+n*n*n+"\n");
}
}
}

Task-4: read and print even numbers from 1 to n.

Sample Input/Output-1:

Enter number: 5
Even numbers from 1 to 5: 2 4

Sample Input/Output-2:

Enter number: 10
Even numbers from 1 to 10: 2 4 6 8 10

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
range = s.nextInt();
System.out.print("Even numbers from 1 to "+range+": ");
for(int i=1; i<=range; i++){
if(i%2 == 0){
System.out.print(i+" ");
}
}
}
}

Task-5: count how many even numbers from 1 to n.

Sample Input/Output-1:

Enter number: 5
There are 2 even numbers from 1 to 5

Sample Input/Output-2:

Enter number: 10
There are 5 even numbers from 1 to 10

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
range = s.nextInt();
int evenCount = 0;
for(int i=1; i<=range; i++){
if(i%2 == 0){
evenCount++;
}
}
System.out.println("There are "+evenCount+" even numbers from 1 to
"+range);
}
}
Task-6: read and find given number is even or not for n different inputs and print
only even numbers count.

Sample Input/Output-1:

Enter number of inputs: 3

Enter number-1: 2
Enter number-2: 5
Enter number-3: 8

There are 2 even numbers for given 3 inputs

Sample Input/Output-2:

Enter number of inputs: 6

Enter number-1: 2
Enter number-2: 5
Enter number-3: 8
Enter number-4: 9
Enter number-5: 4
Enter number-6: 6

There are 4 even numbers for given 6 inputs

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
int evenCount = 0;
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
if(n%2 == 0){
evenCount++;
}
}
System.out.println("There are "+evenCount+" even numbers for given
"+range+" inputs");
}
}

Task-7: read and print sum for 1 to n numbers.

Sample Input/Output-1:

Enter number: 3
Sum for 1 to 3 numbers: 6

Sample Input/Output-2:

Enter number: 5
Sum for 1 to 5 numbers: 15
import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
range = s.nextInt();
int sum = 0;
for(int i=1; i<=range; i++){
sum += i;
}
System.out.println("Sum for 1 to "+range+" numbers: "+sum);
}
}

Task-8: read and print sum of even numbers from 1 to n.

Sample Input/Output-1:

Enter number: 3
Sum of even numbers from 1 to 3: 2

Sample Input/Output-2:

Enter number: 5
Sum of even numbers from 1 to 5: 6

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
range = s.nextInt();
int evenSum = 0;
for(int i=1; i<=range; i++){
if(i%2 == 0){
evenSum += i;
}
}
System.out.println("Sum of even numbers from 1 to "+range+": "+evenSum);
}
}

Task-9: read and print sum for only even numbers for n different inputs.

Sample Input/Output-1:

Enter number of inputs: 3

Enter number-1: 2
Enter number-2: 5
Enter number-3: 8

Even sum for 3 different inputs: 10

Sample Input/Output-2:
Enter number of inputs: 6

Enter number-1: 2
Enter number-2: 5
Enter number-3: 8
Enter number-4: 2
Enter number-5: 10
Enter number-6: 4

Even sum for 6 different inputs: 26

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
int evenSum = 0;
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
if(n%2 == 0){
evenSum += n;
}
}
System.out.println("Even sum for "+range+" different inputs: "+evenSum);
}
}

Task-10: read and print factors for given number.

Sample Input/Output-1:

Enter number: 6
Factors for 6 are: 1 2 3 6

Sample Input/Output-2:

Enter number: 7
Factors for 7 are: 1 7

Sample Input/Output-3:

Enter number: 10
Factors for 10 are: 1 2 5 10

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
System.out.print("Factors for "+n+" are: ");
for(int i=1; i<=n; i++){
if(n%i == 0){
System.out.print(i+" ");
}
}
}
}

Task-11: read and print count of how many factors are there for given number.

Sample Input/Output-1:

Enter number: 6
There are 4 factors for 6

Sample Input/Output-2:

Enter number: 7
There are 2 factors for 7

Sample Input/Output-3:

Enter number: 10
There are 4 factors for 10

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
int factorCount = 0;
for(int i=1; i<=n; i++){
if(n%i == 0){
factorCount++;
}
}
System.out.println("There are "+factorCount+" factors for "+n);
}
}

Task-12: read and print factors, count, sum of all factors for given number.

Sample Input/Output-1:

Enter number: 6
Factors for 6 are: 1 2 3 6
Sum: 12
Count: 4

Sample Input/Output-2:

Enter number: 7
Factors for 7 are: 1 7
Sum: 8
Count: 2

Sample Input/Output-3:

Enter number: 10
Factors for 10 are: 1 2 5 10
Sum: 18
Count: 4

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
int factorCount = 0;
int factorSum = 0;
System.out.print("Factors for "+n+" are: ");
for(int i=1; i<=n; i++){
if(n%i == 0){
System.out.print(i+" ");
factorSum += i;
factorCount++;
}
}
System.out.println("\nSum: "+factorSum+"\nCount: "+factorCount);
}
}

Task-13: read and print factors, total factors count, total factors sum, even
factors count & sum for given number.

Sample Input/Output-1:

Enter number: 6
Factors for 6 are: 1 2 3 6
Total Sum: 12, Even Sum: 8, Odd Sum: 4
Total Count: 4, Even Count: 2, Odd Count: 2

Sample Input/Output-2:

Enter number: 7
Factors for 7 are: 1 7
Total Sum: 8, Even Sum: 0, Odd Sum: 8
Total Count: 2, Even Count: 0, Odd Count: 2

Sample Input/Output-3:

Enter number: 10
Factors for 10 are: 1 2 5 10
Total Sum: 18, Even Sum: 12, Odd Sum: 6
Total Count: 4, Even Count: 2, Odd Count: 2

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();

int totalSum = 0; int evenSum = 0; int oddSum = 0;


int totalCount = 0; int evenCount = 0; int oddCount = 0;
System.out.print("Factors for "+n+" are: ");
for(int i=1; i<=n; i++){
if(n%i == 0){
System.out.print(i+" ");
totalSum += i;
totalCount++;
if(i%2 == 0){
evenSum += i;
evenCount++;
}else{
oddSum += i;
oddCount++;
}
}
}
System.out.println("\nTotal Sum: "+totalSum+", Even Sum: "+evenSum+", Odd
Sum: "+oddSum);
System.out.println("Total Count: "+totalCount+", Even Count: "+evenCount+",
Odd Count: "+oddCount);
}
}

Task-14: read and find given number is prime or not.

Sample Input/Output-1:

Enter number: 7
Yes, 7 is prime number..!

Sample Input/Output-2:

Enter number: 4
No, 4 is not prime number..!

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();

int factorCount = 0;
for(int i=1; i<=n; i++){
if(n%i == 0){
factorCount++;
}
}
if(factorCount == 2){
System.out.println("Yes, "+n+" is prime number..!");
}else{
System.out.println("No, "+n+" is not prime number..!");
}
}
}

Task-15: read and find given number is positive or negative for n different inputs.

Sample Input/Output-1:
Enter number of inputs: 3

Enter number-1: 6
Yes, 6 is positive number

Enter number-2: -6
Yes, -6 is negative number

Enter number-3: 9
Yes, 9 is positive number

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();

for(int i=1; i<=range; i++){


int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
if(n>0){
System.out.println("Yes, "+n+" is positive number\n");
}else{
System.out.println("Yes, "+n+" is negative number\n");
}
}
}
}

Task-16: print multiplication table for given number.

Sample Input/Output-1:

Enter number: 5
Multiplication Table For-5

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
System.out.println("Multiplication Table For - "+n+"\n");
for(int i=1; i<=10; i++){
System.out.println(n+" x "+i+" = "+n*i);
}
}
}

Nested Loop: Defining a loop inside another loop is called 'Nested Loop'. Sometimes
loop can also be repeated in such situtaions to reduce number of duplicate loops we
define Nested Loops.

Task-1: print natural numbers from 1 to given range for n different inputs.

Sample Input/Output:

Enter number of inputs: 3

Enter input-1: 5
Numbers from 1 to 5: 1 2 3 4 5

Enter input-2: 3
Numbers from 1 to 3: 1 2 3

Enter input-3: 7
Numbers from 1 to 7: 1 2 3 4 5 6 7

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
System.out.print("Numbers from 1 to "+range+": ");
for(int j=1; j<=range; j++){
System.out.print(j+" ");
}
System.out.println("\n");
}
}
}

Task-2: print even numbers from 1 to given range for n different inputs.

Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 5
Even numbers from 1 to 5: 2 4

Enter input-2: 10
Numbers from 1 to 10: 2 4 6 8 10

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
System.out.print("Even numbers from 1 to "+range+": ");
for(int j=1; j<=range; j++){
if(j%2 == 0){
System.out.print(j+" ");
}
}
System.out.println("\n");
}
}
}

Task-3: print count of even numbers from 1 to given range for n different inputs.

Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 5
There are 2 even numbers from 1 to 5

Enter input-2: 10
There are 5 even numbers from 1 to 10

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
int evenCount = 0;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
for(int j=1; j<=range; j++){
if(j%2 == 0){
evenCount++;
}
}
System.out.println("There are "+evenCount+" even numbers from 1 to
"+range+"\n");
}
}
}

Task-4: print sum of 1 to given range of natural & even numbers for n different
inputs.
Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 5
Sum for 1 to 5 natural numbers: 15
Sum for 1 to 5 even numbers: 6

Enter input-2: 6
Sum for 1 to 6 natural numbers: 21
Sum for 1 to 6 even numbers: 12

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
int naturalSum = 0;
int evenSum = 0;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
for(int j=1; j<=range; j++){
naturalSum += j;
if(j%2 == 0){
evenSum += j;
}
}
System.out.println("Sum for 1 to "+range+" natural numbers:
"+naturalSum);
System.out.println("Sum for 1 to "+range+" even numbers: "+evenSum+"\
n");
}
}
}

Task-5: print factors from 1 to given range.

Sample Input/Output:

Enter range: 5
Factors for 1: 1
Factors for 2: 1 2
Factors for 3: 1 3
Factors for 4: 1 2 4
Factors for 5: 1 5

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter range: ");
range = s.nextInt();
for(int i=1; i<=range; i++){
System.out.print("Factors for "+i+": ");
for(int j=1; j<=i; j++){
if(i%j == 0){
System.out.print(j+" ");
}
}
System.out.println();
}
}
}

Task-6: print factors for given number for n different inputs.

Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 8
Factors for 8: 1 2 4 8

Enter input-2: 6
Factors for 6: 1 2 3 6

import java.util.Scanner;
class Main{
public static void main(String[] args) {
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
System.out.print("Factors for "+n+": ");
for(int j=1; j<=n; j++){
if(n%j == 0){
System.out.print(j+" ");
}
}
System.out.println("\n");
}
}
}

Task-7: print count, sum of factors of a number for n different inputs.

Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 8
Sum for 8 factors: 15
There are 4 factors for 8

Enter input-2: 6
Sum for 6 factors: 12
There are 4 factors for 6

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
int factorSum = 0;
int factorCount = 0;
for(int j=1; j<=n; j++){
if(n%j == 0){
factorSum += j;
factorCount++;
}
}
System.out.println("Sum for "+n+" factors: "+factorSum);
System.out.println("There are "+factorCount+" factors for "+n+"\n");
}
}
}

Task-8: print prime numbers from 1 to given range.

Sample Input/Output:

Enter range: 10
Prime numbers from 1 to 10: 2 3 5 7

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter range: ");
range = s.nextInt();
System.out.print("Prime numbers from 1 to "+range+": ");
for(int i=1; i<=range; i++){
int factorCount = 0;
for(int j=1; j<=i; j++){
if(i%j == 0){
factorCount++;
}
}
if(factorCount == 2){
System.out.print(i+" ");
}
}
}
}

Task-9: print prime numbers from 1 to given range for n different inputs.

Sample Input/Output:

Enter number of inputs: 2


Enter input-1: 7
Prime numbers from 1 to 7: 2 3 5 7

Enter input-1: 15
Prime numbers from 1 to 15: 2 3 5 7 13

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
System.out.print("Prime numbers from 1 to "+range+": ");
for(int j=1; j<=range; j++){
int factorCount = 0;
for(int k=1; k<=j; k++){
if(j%k == 0){
factorCount++;
}
}
if(factorCount == 2){
System.out.print(j+" ");
}
}
System.out.println("\n");
}
}
}

Task-10: print prime numbers, sum, count from 1 to given range for n different
inputs.

Sample Input/Output:

Enter number of inputs: 2

Enter input-1: 7
Prime numbers from 1 to 7: 2 3 5 7
Count = 4
Sum = 17

Enter input-1: 15
Prime numbers from 1 to 15: 2 3 5 7 13
Count = 5
Sum = 30

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
n = s.nextInt();
for(int i=1; i<=n; i++){
int range;
System.out.print("Enter input-"+i+": ");
range = s.nextInt();
System.out.print("Prime numbers from 1 to "+range+": ");
int primeSum = 0;
int primeCount = 0;
for(int j=1; j<=range; j++){
int factorCount = 0;
for(int k=1; k<=j; k++){
if(j%k == 0){
factorCount++;
}
}
if(factorCount == 2){
System.out.print(j+" ");
primeSum += j;
primeCount++;
}
}
System.out.println("\nCount = "+primeCount+"\nSum = "+primeSum+"\n");
}
}
}

11. print sum of prime numbers for n different inputs. print -1 if there is no
prime in given inputs.

Sample Input/Output-1:

Enter number of inputs: 5

Enter input-1: 2
Enter input-2: 4
Enter input-3: 6
Enter input-4: 7
Enter input-5: 3
Sum of 3 primes of 5 inputs: 12

Sample Input/Output-2:

Enter number of inputs: 3

Enter input-1: 10
Enter input-2: 8
Enter input-3: 6
-1

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
int primeCount = 0;
int primeSum = 0;
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
int factorCount = 0;
for(int j=1; j<=n; j++){
if(n%j == 0){
factorCount++;
}
}
if(factorCount == 2){
primeCount++;
primeSum += n;
}
}
if(primeCount == 0){
System.out.println("-1");
}else{
System.out.println("Sum of "+primeCount+" prime numbers of "+range+"
inputs: "+primeSum);
}
}
}

12. print multiplication tables from 1 to given range.

Sample Input/Output:

Enter range: 5

Multiplication Table For - 1


Multiplication Table For - 2
Multiplication Table For - 3
Multiplication Table For - 4
Multiplication Table For - 5

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter range: ");
range = s.nextInt();
for(int i=1; i<=range; i++){
System.out.println("Multiplication Table For - "+i+":\n");
for(int j=1; j<=10; j++){
System.out.println(i+" x "+j+" = "+i*j);
}
System.out.println();
}
}
}

13. print multiplication tables for n different inputs.

Sample Input/Output:

Enter number of inputs: 3

Enter input-1: 4
Multiplication Table For - 4:
Enter input-2: 3
Multiplication Table For - 3:

Enter input-3: 8
Multiplication Table For - 8:

import java.util.Scanner;
class Main{
public static void main(String[] args){
int range;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of inputs: ");
range = s.nextInt();
for(int i=1; i<=range; i++){
int n;
System.out.print("Enter input-"+i+": ");
n = s.nextInt();
System.out.println("Multiplication Table For - "+n+":\n");
for(int j=1; j<=10; j++){
System.out.println(n+" x "+j+" = "+n*j);
}
System.out.println();
}
}
}

2. while loop syntax:

initialization;
while(condition)
{
while-loop-body
increment/decrement
}

Example-Program:

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
int i=1;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
System.out.print("Numbers from 1 to "+n+": ");
while(i<=n){
System.out.print(i+" ");
i++;
}
}
}

Step-1: initialization: Initialization is outside for while loop. This includes


initialization for iteration variable and loop can be anywhere after
initialization.

Step-2: condition: This part includes an expression. If it is true then block of


statements will be executed. If condition is false then loop stops its iteration
process.

Step-3: while-loop-body & increment/decrement: This part includes both loop-body


and increment/decrement value of iteration variable. After execution of this part,
control again goes to condition.

Step-4: From step-2 to step-3 will be repeated like this until condition becomes
false.

3. do while loop syntax:

initialization;

do
{
while-loop-body
increment/decrement
}
while(condition);

Example-Program:

import java.util.Scanner;
class Main{
public static void main(String[] args){
int n;
int i=1;
Scanner s = new Scanner(System.in);
System.out.print("Enter number: ");
n = s.nextInt();
System.out.print("Numbers from 1 to "+n+": ");
do{
System.out.print(i+" ");
i++;
}while(i<=n);
}
}

Step-1: initialization: Initialization is outside for while loop. This includes


initialization for iteration variable and loop can be anywhere after
initialization.

Step-2: do-while-loop-body & increment/decrement: This part includes both loop-body


and increment/decrement value of iteration variable. After execution of this part,
control again goes to condition.

Step-3: condition: This part includes an expression. If it is true then block of


statements will be executed again. If condition is false then loop stops its
iteration process.

Step-4: From step-2 to step-3 will be repeated like this until condition becomes
false.

NOTE-1:

for & while: These both loops won't get executed if the condition is false in the
begining. Hence these two are also called as 'Entry Controlled Looping Statements'.

NOTE-2:
do-while: This loop gets executed even if the condition is false in the begining.
Hence this is also called as 'Exit Controlled Looping Statement'.

4. for-each loop syntax:

for(DataType variableName:array/collection)
{
//code
}

Example-Program-1: Array

class Main{
public static void main(String[] args){
int array[] = {8, 5, 9, 6};
for(int val:array)
{
System.out.print(val+" ");
}
}
}

Example-Program-2: Collection

import java.util.ArrayList;
class Main{
public static void main(String[] args){
ArrayList<String> list = new ArrayList<>();
list.add("Thirupal sir");
list.add("Naresh sir");
list.add("Ajitha madam");
list.add("Prabhakar sir");

for(String val:list)
{
System.out.println(val);
}
}
}

You might also like