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

Loops in Java

Uploaded by

aratrey63
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Loops in Java

Uploaded by

aratrey63
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Loops in Java

1. Initialization: - It is the initial condition which is executed once when the


loop starts. Here, we can initialize the variable, or we can use an already
initialized variable. It is an optional condition.
2. Condition: - It is the second condition which is executed each time to test
the condition of the loop. It continues execution until the condition is false. It
must return boolean value either true or false. It is an optional condition.
3. Increment/Decrement:- It increments or decrements the variable value. It is
an optional condition.
4. Statement:- The statement of the loop is executed each time until the second
condition is false.

Syntax:
for(initialization; condition; increment/decrement){
//statement or code to be executed
}

------------------------------------------------------------For
Loop-------------------------------------------------------
//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

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

public class PyramidExample {


public static void main(String[] args) {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
output :-
*
* *
* * *
* * * *
* * * * *

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--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}

Output:

* * * * * *
* * * * *
* * * *
* * *
* *
*
-------------------------------------------------For-Each
Loop------------------------------------------

Syntax:

for(data_type variable : array_name){


//code to be executed
}

//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

------------------------------------------Infinitive for
Loop--------------------------------------
Syntax:

for(;;){
//code to be executed
}

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


//which prints an statement
public class ForExample {
public static void main(String[] args) {
//Using no condition in for loop
for(;;){
System.out.println("Vijay");
// Stop loop to press ctrl+C
}
}
}

------------------------------------------------------While
Loop-------------------------------------
Syntax:

while (condition){
//code to be executed
I ncrement / decrement statement
}

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

Infinitive While Loop


Syntax:

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

public class WhileExample2 {


public static void main(String[] args) {
// setting the infinite while loop by passing true to the condition
while(true){
System.out.println("Vijay");
// Stop loop to press ctrl+C
}
}
}

--------------------------------------------do-while
Loop---------------------------------
Syntax:

do{
//code to be executed / loop body
//update statement
}while (condition);

public class DoWhileExample {


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

OUTPUT:-
1
2
3
4
5
6
7
8
9
10

Infinitive do-while Loop

Syntax:
do{
//code to be executed
}while(true);

public class DoWhileExample2 {


public static void main(String[] args) {
do{
System.out.println("infinitive do while loop");
}while(true);
}
}

You might also like