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

Java Looping Statements Notes

The document discusses various looping statements in Java including for, while, do-while loops and nested loops. It provides examples of using these loops to print numbers, patterns and traverse arrays. Key points covered include using a for loop to print numbers from 1 to 10, nested for loops to print multiple values, and using a do-while loop where the condition is checked at the end of each iteration. Labeled loops and infinite loops are also demonstrated.

Uploaded by

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

Java Looping Statements Notes

The document discusses various looping statements in Java including for, while, do-while loops and nested loops. It provides examples of using these loops to print numbers, patterns and traverse arrays. Key points covered include using a for loop to print numbers from 1 to 10, nested for loops to print multiple values, and using a do-while loop where the condition is checked at the end of each iteration. Labeled loops and infinite loops are also demonstrated.

Uploaded by

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

Looping or iterative Statements

Exercise:- Write a program to print number from 1 to 10 for


loop.(Use of simple for loop).

ForExample.java

//Java Program to demonstrate the example of for loop


//which prints table of 1
public class ForExample {
public static void main(String[] args) {
//Code of Java for loop
for(int i=1;i<=10;i++){
System.out.println(i);
}
}
}

Output:

1
2
3
4
5
6
7
8
9
10

Exercise:- Write a Java program to show working of nested for


loop.

NestedForExample.java

public class NestedForExample {


public static void main(String[] args) {
//loop of i
for(int i=1;i<=3;i++){
//loop of j
for(int j=1;j<=3;j++){
System.out.println(i+" "+j);
}//end of i
}//end of j
}
}

Output:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

Exercise:- Write a Java program to print a pyramid pattern of stars


using nested for loop.

PyramidExample2.java

public class PyramidExample2 {


public static void main(String[] args) {
int term=6;
for(int i=1;i<=term;i++){
for(int j=term;j>=i;j--){ // use (int j=term;j<=term;j++) for ascending pattern
System.out.print("* ");
}
System.out.println();//new line
}
}
}

Output:

* * * * * *
* * * * *
* * * *
* * *
* *
*
Exercise:- Program to access array elements using for each
loop.

It is easier to use for-each loop to traverse array or collection in Java

Syntax:

for(data_type variable : array_name){


//code to be executed
}

ForEachExample.java

//Java For-each loop example which prints the


//elements of the array
public class ForEachExample {
public static void main(String[] args) {
//Declaring an array
int arr[]={12,23,44,56,78};
//Printing array using for-each loop
for(int i:arr){
System.out.println(i);
}
}
}

Output:

12
23
44
56
78

Java Labeled For Loop


We can have a name of each Java for loop. To do so, we use label before the for loop.
It is useful while using the nested for loop as we can break/continue specific for loop.

Note: The break and continue keywords breaks or continues the innermost for loop
respectively.

Syntax:
1. labelname:
2. for(initialization; condition; increment/decrement){
3. //code to be executed
4. }

LabeledForExample2.java

public class LabeledForExample2 {


public static void main(String[] args) {
aa:
for(int i=1;i<=3;i++){
bb:
for(int j=1;j<=3;j++){
if(i==2&&j==2){
break bb;
}
System.out.println(i+" "+j);
}
}
}
}

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Exercise:- Program in java to show use of Java Infinitive for Loop.


ForExampleInfinite.java

//Java program to demonstrate the use of infinite for loop


//which prints an statement
public class ForExampleInfinite {
public static void main(String[] args) {
//Using no condition in for loop
for(;;){
System.out.println("infinitive loop");
}
}
}

Output:

infinitive loop
infinitive loop
infinitive loop
infinitive loop
infinitive loop
ctrl+c

Now, you need to press ctrl+c to exit from the program.

While and Do …while loops


Exercise:- Write Java program to show use of while loop.
WhileExample.java

public class WhileExample {


public static void main(String[] args) {
int i=1;
while(i<=10){
System.out.println(i);
i++;
}
}
}

Output:

1
2
3
4
5
6
7
8
9
10
Exercise:- Write Java program to show use of Infinitive While
Loop.

If you pass true in the while loop, it will be infinitive while loop.

Syntax:

while(true){
//code to be executed
}

WhileExample2.java

1. public class WhileExample2 {


2. public static void main(String[] args) {
3. // setting the infinite while loop by passing true to the condition
4. while(true){
5. System.out.println("infinitive while loop");
6. }
7. }
8. }

Output:

infinitive while loop


infinitive while loop
infinitive while loop
infinitive while loop
infinitive while loop
ctrl+c

In the above code, we need to enter Ctrl + C command to terminate the infinite loop.

Exercise:- Write program in java to show use of do …. while loop.


DoWhileExample.java

public class DoWhileExample {


public static void main(String[] args) {
int i=1; // if we use 11 then loop will execute once as condition tested in end.
do{
System.out.println(i);
i++;
}while(i<=10);
}
}

Output:

1
2
3
4
5
6
7
8
9
10

You might also like